Skip to main content

Full text of "VIC-20 Programmers Reference Guide 1st Edition 6th Printing"

See other formats


fr 




s® 



All all-purpose reference 
guide for first-time 
I computerists as well 
as experienced 
prograiHiners! 



PROGRA^ER'S 

REFERENCE 
GUIDE 




*f 



■** 



f S commodore 

COMPUTER 



VM110 



L 



[ 



r 







Reference 



A. Flnkel 

N. Harris 

P. Higginbottom 

M. Tomczyk 



Published by 

Commodore Business Machines, Inc. 
and Howard W. Sams & Co., Inc. 



FIRST EDITION 
SIXTH PRINTING— 1983 



Copyright s 1982 by Commodore Business Machines. Inc. 

All rights reserved 

No pari of this publication may be reproduced, stored in a retrievai system, or 

transmitted in any form or by any means, electronic, mechanical, photocopying. 

recording or otherwise without the prior written permission of Commodore 

Business Machines, Inc. 



L 



L 



TABLE OF CONTENTS 



INTRODUCING THE PROGRAMMER'S REFERENCE 

GUIDE V 

VIC 20 APPUCATIONS GUIDE vii 

1 BASIC PROGRAMMING REFERENCE GUIDE i 

• VIC BASIC: The Language of the VIC 3 

• Commands 5 

• Statements 14 

• I/O Statements 35 

• BASIC Functions 40 

• Numbers and Variables 54 

• Operators 62 

• Logical Operators 68 

2 PROGRAMMING TIPS 71 

• Editing Programs.... 73 

• Using the GET Statement .,. 77 

• How to Cruncli BASIC Programs.. 79 

• Working Witli Graphics ......82 

Character Memory .,.. 82 

Programmable Characters ,...82 

High Resolution Graphics 88 

Multi-Coior Mode Graphics,, 92 

Superexpander Cartridge ......94 

• Sound and Music 95 

3 MACHINE LANGUAGE PROGRAMMING GUIDE m 

• System Overview 109 

• Introduction to Machine Language 123 

• Writing Your First Program 132 

lii 



• special Tips for Beginners.. ...*.. , .168 

• Memory Maps , 170 

• Useful Memory Locations .,„ ..„.,.. 178 

• The KERNAL 182 

• KERNAL Power Up Activities .211 

• VIC Chips ,... ._ ....212 

6560 (Video Interface Chip) 212 

6522 {Versatile Interlace Adapter).. 218 

4 INPUT/OUTPUT GUIDE 227 

• User Port.... 229 

• The Serial Bus 234 

• Using the VIC Graphic Printer 236 

• VIC Expansion Port 241 

• Game Controllers 246 

Joystick..-.. 246 

Paddles .„. .248 

Light Pen.... 250 

• RS-232 Interlace Descriptjon ...251 

APPENDICES 261 

A. Abbreviations for BASIC Keywords..... 263 

B. Screen & Border Color Combinations 265 

C. Table of Musical Notes 266 

D. Screen Display Codes 267 

E. Screen Memory Maps. 270 

F. ASCII and CHRS Codes 272 

G. Deriving Mathematical Functions....... 275 

H. Error Messages... 276 

I. Converting Programs to VIC 20 BASIC 278 

J. Pinouts for Input/Output Devices 280 

K. VIC Peripherais & Accessories ...284 

INDEX 285 

SCHEMATIC 291 



[ 



L. 



iv 



INTRODUCING. . . 

THE PROGRAMMER'S 

REFERENCE GUIDEI 



The Friendly Computer deserves a Friendly Reference Book. 

That's why we wrote the VIC 20 PROGRAMMER'S REFER- 
ENCE GUIDE ... a book that gives you more information about 
your VIC 20 Personal Computer than any other source. This guide 
was compiled from the experience of Commodore's international 
programming staffs in more than half a dozen countries, and is 
designed to be used by first-time computerists as weli as 
experienced programmers. 

To cover the areas VIC 20 programmers are most Interested in, 
we divided the book into four sections: BASIC Programming, 
Machine Language Programming, Input/Output Interfacing and 
Programming Graphics & Sound. 

Here are just a few of the ways the VIC 20 Programmer's 
Reference Guide helps meet your programming needs: 

— Our complete "dictionary' includes not only BASIC com- 
mands but also sample programs to show you how they work. 

—Need an introduction to Machine Level Programming? Our 
laymen^s overview gets you started. 

—The exclusive Kernel helps assure the programs you write 
today won't be outdated tomorrow, 

— The VIC'S Interfacesectionletsyou expand your computer. . 
from RS232 for telecommunications to joysticks, game paddles 
and lightpens, 

— You'll have fun learning about the VIC's graphic, sound and 
music capabilities . . . including the unique "multicolor" mode, 

— You'll discover POKEs you never knew about, and probably 
PEEK into some memory locations you never knew existed. 



There are lots of fascinating liours ahead of you. Let the 
Prog ram mefs Reference Guide be your companion as you 
continue to explore your VIC 20 Personal Computer System. 

And ... if you find any errors in this book, please send us a 
postcard or letter in care of VIC PROGRAMMER'S REFERENCE 
GUIDE, VIC Product Marketing Group, Commodore Business 
Machines, Inc., 681 Moore Road, King of Prussia, PA 19406. We'd 
appreciate your assistance in helping us ''debug" our reference 
guide for future printings. 

Enjoy your new reference guide . . . and happy programming! 

— The Authors 



r 



r 



[ 



vi 



L 
[ 
[ 
[ 
[ 
L 

r 
[ 
[ 
[ 

L 
[ 
L 
[ 



VIC 20 APPLICATIONS GUIDE 



When you first considered buying a computer, the chances are 
you said something [ike, "I l<now computers are good things to have 
and it's nice that they're finally affordable, but . . . what can I do with 
one?'^ 

The great thing about a computer is that you can tailor the 
machine to do what you want it to — you can make it calculate your 
home budget, play arcade-style action games — you can even 
make it talk! And the best thing is, if your VIC 20 does only ONE of 
the things listed below, it's well worth the price you paid for It. 

Here then, is a list of applications for your VIC 20— in case you Ve 
asked yourself, "Yes, but what else can I do with it?" 



APPLICATION 



COMMENTS/REQUIRE- 
MENTS 



ADVENTURE 
GAMES 



ADVERTISING & 
MERCHANDISING 



ANIMATION 



BABYSITTING 



COMMODORE provides 5 Scott 
Adams Adventure games on car- 
tridge, decoded to "talk" with the 
VOTRAX 'Type N Talk"^. 

Hook the VIC to a television and put 
it in a store window with an animated 
message flashing and you've got a 
great point of purchase store dis- 
play. 

The VIC is well-suited to screen 
animation ... a special aid called 
THE PROGRAMMABLE CHARAC- 
TER SET & GAMEGRAPHICS 
EDITOR is available from COM- 
MODORE on tape cassette. 

The VIC HOME BABYSITTER car- 
tridge can keep your child occupied 
for hours and teach keyboard sym- 
bols, special learning concepts and 
relationships. A "first" from COM- 
MODORE. 



BASIC 
PROGRAMMING 



BfORHYTHM 
CHARTING 



CHESS GAME 



COLLECTIONS 



COMMUNfCATlON 



COMPOSING 
SONGS 



DEXTERITY 



The VIC owner's guide and the 
TEACH YOURSELF PROGRAM- 
MING series of books and tapes are 
excellent starting points. A PRO- 
GRAMMERS AID CARTRIDGE is 
available from COMMODORE. 

COMMODORE'S Biorhythm pro- 
gram on tape has a special compati- 
bility feature whiich lets you compare 
yourself to anyone else by simply 
typing in your birthdates. 

SARGON II (on cartridge from 
COMMODORE) has been called 
the most powerful microcomputer 
chess program anywhere, 

COMMODORE will provide a car- 
tridge which allows coilectors to 
record their collections (stamps, 
coins or other items) on tape or 
floppy diskettes, and print out these 
lists on the VIC GRAPHIC PRINT- 
ER. 

VICMODEM^-, VICNET- and VIC- 
TERM^"^ are ail products which allow 
VIC owners to communicate by 
telephone with other computer 
owners, or telecomputing services 
like CompuServe '"^ or The Source-'". 

The VIC s 3 tone generators cover 5 
octaves and may be used to write 
and record music. The best music 
writing accessory is the SUPEREX- 
PANDER CARTRIDGE which lets 
you write music in note form and 
save it on tape or disk. 

Hand-to-eye coordination and man- 
ual dexterity are aided by several of 
COMMODORE'S VIC games . . . 
including the "Jupiter Lander" and 
night driving simulations, among 
others. 



L 



L 



L 



EDUCATION 



L 



EXPENSE 
RECORDS 

FOREIGN 
LANGUAGE 



FORMULA/FIGURES 



GAMBLING 



GAMES 



GRAPHICS 
PLOTTING 



L 

L 



The COMMODORE Educational 
Computing Resouroe Book con- 
tains information on educational 
uses of computers in general as well 
as educational sof^are lists for the 
VIC 20. Available through COM- 
MODORE computer dealers. 

A CALENDAR/EXPENSE REC- 
ORD tape is offered by COMMO- 
DORE. 

The VIC Programmable Character 
Set Editor lets any user replace the 
VIC character set with user-defined 
foreign language characters. 

The VIC has the same powerful 
math routines built into its operating 
system as the COMMODORE 
PET/CBM microcomputers. Com- 
plex formulas may be calculated 
quickly and easily either directly or 
under program control (see the 
OPERATORS section of the VIC 
user manual and/or the matching 
section in this book). 

COMMODORE provides several 
games which provide hours of gam- 
bling fun without risking any money 
. . . programs like VIC21 Casino 
Style Blackjack (on tape), SU- 
PERSLOT (cartridge) and DRAW 
POKER (cartridge). 

Everything from space games on 
cartridge to Bfackjack on tape, plus 
REAL ARCADE games adapted 
from the most popular coin-operat- 
ed games in the world. 

The SUPEREXPANDER CAR- 
TRIDGE offers 3K memory expan- 
sion, hi-resolution multi-coJor 
graphics plotting, easy function key 
definition, and musicwriting com- 
mands . . , all in one cartridge. 

xi 



HOME INVENTORY 



INSTRUMENT 
CONTROL 



JOURNALS OR 

CREATIVE 

WRITING 



LIGHTPEN 
CONTROL 



LOAN/MORTGAGE 
CALCULATION 

MACHINE CODE 
PROGRAMMING 



MATH PRACTICE 
TOOL 



The HOME INVENTORY tape in 
COMMODORE'S HOME CALCU- 
LATION SIXPACK provides a low 
priced method for storing and up- 
dating lists of belongings for insur- 
ance purposes, business purposes, 
etc. 

The VIC has a serial port, RS-232 
port and IEEE-4888 adapter car- 
tridge for use in a variety of special 
industrial applications. 

The VIC is excellent for making daily 
journal entries, using the VIC TYPE- 
WRITER or VICWRITER. Informa- 
tion can be stored on the VIC 
DATASSETTE tape recorder or VIC 
DISK DRIVE, and printed out on a 
VIC GRAPHIC PRINTER. 

Applications using a lightpen to 
specify items can use any commer- 
cial lightpen which fits the VIC game 
port connector ... at least two 
makers market lightpens which 
work with the VIC. 

Try the LOAN/MORTGAGE CAL- 
CULATOR from COMMODORE. 

COMMODORE'S PROGRAM- 
MER'S REFERENCE GUIDE in- 
cludes a machine language section. 
The VICMON'^ machine language 
monitor cartridge is recommended. 
VIC machine language programs 
may also be written in assembly 
language on the PET/CBM using the 
COMMODORE Assembler Develop- 
ment System. 

Several software companies offer 
educational programs on tape for 
the VIC. COMMODORE^s first math 
practice program, called "SPACE- 
MATH," is available on tape. 



L 



[ 



[ 



L, 



^1 



NETWORKING 8l 

DISTRIBUTED 

PROCESSfNG 



PAYROLL & 
FORMS PRINTOUT 



PERSONAL 
BUDGET 

PORTFOLIO 
ANALYSIS 



PRINT 

INFORMATION ON 
PAPER 



RECIPES 



SIMULATIONS 



SPORTS DATA 



Networking may be achieved by 
using the VIC as part of a telephone 
or RS-232 hookup, or by using a 
commercially available network 
system. 

The VIC can be programmed to 
handle a variety of entry-type busi* 
ness applications. Upper/lower 
case letters combined with VIC 
^'business form^' graphics make it 
easy to design forms, which can be 
easily printed out on the VIC 
GRAPHIC PRINTER. 

COMMODORE provides PERSON- 
AL FINANCE programs on tape and 
on plug-in cartridge. 

Business software which performs 
this function is available as printed 
programs in books available from 
most computer stores. This service 
is also available through telecom- 
puting services. 

The VIC GRAPHIC PRINTER prints 
letters, numbers and graphics in 
high quality dot matrix format, RS- 
232 PRINTERS including letter 
quality printers may also be used 
with the proper interlacing. An IEEE 
488 INTERFACE cartridge may also 
allow IEEE printer use. 

See the recipe program called 
"MIKE^S CHICKEN SOUP" in the 
VIC owners manual, or check a 
computer book rack (most "practi- 
cal" program books contain recipe 
programs). 

Computer simulations permit dan- 
gerous or expensive experiments to 
be performed at minimum risk & 
expense. 

The Source'^ and CompuServe'^ 
both provide sports information. 



Xtll 



STOCK QUOTES 



TALKING VIC 



TERM PAPERS 
& REPORTS 



TERMINAL & 
MODEM 



TYPING PRACTICE 



WORDPROCESSING 



The VIC, a modem, and a subscrip- 
tion to The Source"'' or Compu- 
Serve'^ can cost less than S500. 

Connect the VIC to a voice synthe- 
sizer such as the 'Type N Talk"'^ 
manufactured by VOTRAX INC. 

The VIC helps students research 
current library-type sources over the 
telephone . . . and compose, edit 
and print out their reports on the VIC 
and VIC GRAPHIC PRINTER . . . 
the same type of computer services 
which were previously available 
only through large institutions at a 
cost of many thousands of dollars. 

VIC accessories include an RS-232 
modem interlace (for use with RS- 
232 modems) or the ultra low-priced 
VICMODEM^" 

The reverse side of the VIC TYPE- 
WRITER has a "TYPING TUTOR" 
program. 

THE VIC TYPEWRITERS is avail- 
able on tape and the VIC- 
WRITER'" cartridge also provides 
word processing power. Both work 
with the VIC GRAPHIC PRINTER. 



XIV 



BASIC PROGRAMMING 
REFERENCE GUIDE 

• VIC BASIC: The Language 
of the VIC 

• Commands 
Statements 

> I/O Statements 
BASIC Functions 

• Numbers and Variables 

• Logical Operators 



[ 

L 
[ 

[ 



L 

r 



L 
L 
[ 
[ 
L 



VIC BASIC: THE LANGUAGE OF 
THE VIC 

The BASIC computing language is a powerful and easy-to-use 
means of communicating instructions to your VIC 20 Personal 
Computer. VIC BASIC is the same language used in the 
Commodore PET/CBM line ot microcomputers, and is nearly 
identical to the BASIC used in most other personal computers. 
Learning BASIC now can prepare you to move up to a more 
sophisticated computer in the future, and can also give you the 
foundation you need to learn other ''higher level" computing 
languages. 

II you're a first-time computarlst, you1i be pleased to know you 
can write your first BASIC program on the VIC within 15 minutes, 
using the VIC 20 PERSONAL COMPUTER GUIDE which comes 
with the machine. Additional self-teaching aids are available from 
Commodore as part of the TEACH YOURSELF PROGRAMMING 
SERIES, and ci asses offered by schools, computer centers and 
retail stores can give you a soiid grounding in the fundamentals of 
BASIC within 4-6 hours. 

The VIC BASIC instructions which follow will provide a valuable 
reference as you learn to write BASIC, or as you put into practice 
the techniques you've already learned. Each entry in the listing 
explains how the instruction is used, with practical examples. 
Additional programming tips are included in a separate "BASIC 
PROGRAMMING TIPS" section. 

BASIC has approximateiy 60 words fn its vocabulary and is 
surprisingly easy to learn. That doesn't mean you can't keep 
improving, however. Like any language, BASIC has its own 
"idioms" and complexities which you can use to write increasingly 
sophisticated programs, VIC BASIC even has a sort of "slang" in 
that you can abbreviate most of the commands by typing the first 
letter of the instruction and the SHIFTED second letter. Using 
abbreviated commands to write programs makes programming the 
VIC fast and convenient, (Note that if you LIST a program written in 
abbreviated form, the full-length commands are displayed to help 
you read your program.) 

In BASIC, ail instructions are commonly referred to as 
"commands/' although technically the BASIC instruction set can 
be broken down into several areas . . . which is how weVe grouped 
them in the following VIC BASIC "vocabulary"^ guide. We've 
included separate sections on several types of BASIC instructions: 
Commands, Statements, inputOutput Statements, Functions, 
Numbers and Variables, and Operators. 



It should also be noted here that while we communicate with the 
VIC through the BASIC language, the VIC's "native'^ vocabulary is 
Machine Language which is based on a binary or hexadecimal 
numbering system. BASIC is really a translation of Machine 
Language into terms we humans can understand , . . which is why 
BASIC programs generally run slower than Machine Language 
programs, since BASIC programs have to be interpreted into 
Machine Language before they run, while Machine Language 
programs run immediately. See "Introduction to Machine Lan- 
guage Programming^' for more information. 

Of course, you don^t have to know BASIC to take advantage of 

the VIC'S computing power you don t even have to know how to 

type. We like to say that with the VIC 20, the first thing you do is 
leam "computing" ... not '^programming." You don't have to be an 
auto mechanic to drive a car, and by the same token you don't have 
to be a programmer to "drive" your VIC 20. Still, knowing something 
about how your car works mechanically helps you maintain and use 
your carlo best advantage. Likewise, knowing how the computer is 
programmed helps you get the most out of your VIC 20. 

In the future, being able to "speak'' a computer language will give 
you a tremendous advantage over those who can't , . . not because 
you can write a computer program, but because you'll have a better 
understanding of what a computer is and does, and you'll be able to 
make better use of computing at school, on the job and at home. 
Learning BASIC . , . or at least how it works . . .will bring you close 
to the future and prepare you for the dramatic technological 
changes that are already occurring as part of the "Computer 
Revolution." 



TO AVOID CONFUSION, PLEASE NOTE: 




= letter as in OPEN 




- ZERO 




1 = letter 1 as In INPUT 




1 = number ONE 




* = type the large asterisk key 




Format = all format entries shall be typed in 


on one line 


[ ] = information contained inside brackets in 


the Format 


lines is optionai. 





WARM START~lf you get into trouble or want to break out 
of a program while it's running, you can hold down the 
RUN/STOP key and hit the RESTORE key. This combination 
resets the VIC without losing your program. Now you can 
LIST or RUN your program again. 



r 

L 



[ 

L 



r 



COMMANDS 

BASIC commands tell the VIC to do something with a. program. 
For example, you "command" the VIC to list, run, stop, continue, 
save and load programs. 

BASIC commands may be used in direct mode (without line 
numbers) or as instructions in BASIC programs. In direct mode, 
commands are executed as soon as the RETURN key is pressed. 

In a BASIC program, commands are included like any other 
instruction, and executed when you type RUN. The only command 
which may not be used in a program is CONT. 

V)C BASIC commands mclude the following: 

CONT 

LIST 

LOAD 

NEW 

RUN 

SAVE 

VERIFY 

CONT 

Format : Abbreviation : Screen Disptay: 

CONT clSffBlo on 



This command is used to re-start the execution of a program 
which has been stopped by either using the STOP key, a STOP 
statement, or an END statement within the program. The program 
wilf re-start at the exact place from which it left off. While the 
program is stopped, the user can inspect or change any variables 
or look at the program. CONT will not work if you have changed or 
added lines of the program (or even just moved the cursor to a 
program line and hit RETURN without changing anything), or if the 
program halted due to an error, or if you caused an error before 
typing to re-start tlie program. The message in this case is CANT 
CONTINUE ERROR. 

This is a handy tool when debugging a program. It lets you place 
STOP statements at strategic locations in the program, and 
examine variable values when the program stops. You can keep 
using STOP and CONT until you find what you're looking for 



EXAMPLE 

10 PI ^ 0; C = 1 



I u r I — u , ^j = I t 

20 PI = PI -f 4/C - 4/{C ^ 2) 

30 PRINT PI ^ 

40 C = C -H 4:G0T0 20 

This program calculates t he va lue of PL RUN this program, and 
atter a short while hit the ^Q key. You will see the display: r 

BREAK IN 20 | 

Type the command PRINT C to see how far the VfC has gotten. 
Then use CONT to resume from where the VIC left off. I 

LIST 

Format :* Abbreviation : Screen Display: 

LIST [from line number] L ^^^9 I L LI 

- [to line number] 



The LIST command may also be used as a statement within a 
BASIC program, but the program will stop as soon as the LIST is 
finished. 

*AII format entries should be typed m one line. 

6 



L 



The LIST command allows you to look at lines of the BASIC 
program currently in the VlC's memory. The VIC^s powerful screen 
editor allows you to easily and quickly edit programs which you 
have LISTed. 

The LIST command can specify which program line numbers will 1 

be shown. If you type LIST and a single line number, only that line \ 

will be displayed. If the number is foilowed by a hyphen (-), all the 
lines from that number forward wifl be shown. If the number is , 

preceded by the hyphen, all lines from the beginning to that line will 
be shown. You can LIST a range of line numbers by typing the two ^ 

line numbers separated by a hyphen, in which case the lines from 
the first number to the second number are shown. If LIST is typed, 
the whole program will be LfSTed and not just line 0. 

If the program length exceeds the length of the screen display, 

the first lines in the program wiil scroll off th e screen du nng LISTing. 

To slow down the scrolling, hold down the ^^^H key. To stop 

the program during a LIST, hit the ^9 key- 



1 



1 

L 

L 



EXAMPLES OF LIST COMMAND 

LIST LISTS whole program 

LfST LISTS whole program 

LIST 100 Line 100 only 

LIST 100- Everything from 100 on 

LIST -100 Shows trom start to 100 

LIST 100-150 Starts at 100 and stops with 150 

EXAMPLE OF LIST STATEMENT 

(Program Mode) 

10 PRINTTHIS IS LINE 10" 

20 LIST 

30 PRINTTHIS IS LINE 30" 

LOAD 

^oi^n^at : Abbreviation : Screen Display: 

LOAD [-lile name^ L ^^Q O L □ 

device , command ] ^■■■^ 

The LOAD command transfers a program from cassette tape or 
disk into the VIC^s memory, where it can be used or changed. 

LOAD FROM TAPE 

If the program to be LOADed is the first one on the tape, al[ you 
have to type is the word LOAD by itself. Unless the PLAY key was 
already down, the VIC answers with the message: 

PRESS PLAY ON TAPE. 

Once the recorder has been started, the VIC says: 
OK 

SEARCHING 
FOUND 
LOADING 

At this point, any program that had been in memory is lost, 
because the new one has begun to take its place. (If you use the 

HjH key to halt the LOAD, there will Jikely be a spot in the 
program with garbage, just at the point where you stopped it.) 

Once the program has finished the LOAD, the VIC says: 
READY. 



When the program is not the first on the tape, or you're not sure 
that it is, the VIC can search for the program you want. Type LOAD 
and the name of the program inside quote marks ("), or the name of 
a string variable containing the name of the program. The VIC will 
show any other programs or files that it sees on the tape with the 
message: 

FOUND name 

The VIC will only LOAD the correct program, and will not LOAD a 
data file on that tape. 

LOAD FROM DISK 

In order to bring in a program from a device other than the tape, a 
device number is used. FolEowing the name of the program, type a 
comma {,) and the number (or variable containing the number). The 
cassette is device number 1 . The diskdrive is device numbers. See 
the manual of the device for its number. 

If the program with that name is not found on the device, a FILE 
NOT FOUND ERROR will result. This doesn't happen on tape, 
since the VIC has no way of sensing that there are no more 
programs on a tape. It is possible to put an end-of-tape marker on 
the tape, using the SAVE or OPEN statements, and if the VIC reads 
this marker while searching a tape, an error message appears. 

The VIC will automatically LOAD the program into the beginning 
of BASIC program memory, at location 4096 in a machine without 
an extra 3K of memory, or at 1024 with the extra 3K, For certain 
applications this may not be convenient. By toliowing the device 
number with a comma and the command number 1 , the VIC will be 
sure to LOAD the program in the same spot in memory from which it 
was SAVEd. 

LOAD can be used as a statement in a BASIC program. The 
program will be RUN as soon as LOADIng is finished. Variables 
used in the first program will not be cleared as long as the new 
program is shorter in length than the older one. If a longer program 
is LOADed on a short one, the BASIC program lines will over-write 
the variables, and changing a variable will mess up the program. 

Note that using an asterisk can save loading time {see 
examples). 

EXAMPLES: 

LOAD Reads in the next program from tape. 

LOAD "HELLO" Searches tape until the program called 

HELLO is found, then it is LOADed. 
LOAD A$ Uses the name in A$ to search. 

i 



r 

L 
F 



[ 



r 

L 



LOAD "*", 8 LOADS first program from disk. 

LOAD "AB*'\ 8 Loads first program beginning with AB. 

LOAD "HELLO^", 8 Looks for a program on device 8 (disk 

drive). 
LOAD '"',1,1 Looks for the first program on tape, and 

LOADS it into the same part of memory that 

it came from. 
1 LOAD''NEXT\ 8 Finds the program called NEXT on device 

8, LOADS, then RUNs it. 

Because of possible problems with old tapes or misaligned 
recorders, it is possible that the program will not LOAD correctly. 
The VIC stores two copies of the program on the tape. II they don't 
match, the message LOAD ERROR is displayed. The program may 
LIST correctly, but probably won't. In any case, there is most likely 
some problem, some section of memory that is not right, and the 
program should be re-LOADed. It is wise to make an extra copy of 
any program, in case you run into this problem at some time. 

In the case of a very bad LOAD, some of the important memory 
locations inside the VIC may be changed. If you get weird results 
after a LOAD, like the VIC not understanding normal BASIC 
commands anymore, you1l have to turn the VIC off and then on 
again. 

NEW 

Format : Abbreviation : Screen Display: 

NEW MQne None 

NEW IS used to tell the VIC to erase a current program from 
memory so a different program can be used. Unless the program is 
stored (on tape or disk), it wiil be lost unless it is typed in again from 
the beginning. For this reason, you should BE CAREFUL when 
using this command! Not clearing out an old program before typing 
in a new one can result in a confusing mixing of the two programs. 

NEW can also be used as a statement within a program. When 
this instruction is executed, the program in memory is erased, and 
the program stops. This is not a good programming technique, 
especially if you RUN the program and it erases itself while you're 
writing or debugging it. 

EXAMPLE: 

NEW Clears the program & variables. 

10 NEW Performs the NEW operation and stops the program. 



RUN 

Format : Abbreviation : Screen Display: 

RUN [line number] R KfflBM U R Q 



This command causes a BASIC program to begin operating. The 
command RUN by itself starts the program at the lowest numbered 
fine. All variable values are cleared when this command is given. 

RUN followed by a number causes the program to start working 
from another line than the lowest numbered one. If that line number 
does not exist, the message UNDEF'D STATEMENT ERROR 
appears. RUN followed by a variable will first clear the value of that 
variable, and try to start the program at line 0, if it exists. 

RUN can also be used as a statement within a program. Keep in 
mind that all variables are cleared when this statement is executed. 

EXAMPLES: 

RUN Starts at the beginning 

RUN 100 Starts at line 100 

RUN X Starts at line 0, or UNDEF^D 

STATEMENT ERROR if no line 

SAVE 

Format: Abbreviation : Screen Dispiay: 

SAVE [ "filename" . S^^^J A S ^ 

device , command ] 

The SAVE command stores a program currently in memory on 
tape or disk. The program being SAVEd is not affected and remains 
in the VIC'S memory after the save operation. Programs on tape are 
stored twice automatically, so the VIC can check lor errors when 
LOADing the program back in. 

The command SAVE all by itself sends the program to the 
cassette deck without a name. When the command is given, the 
ViC will say: 

PRESS RECORD AND PLAY ON TAPE 

Holding down the RECORD button, press PLAY, and the VIC will 
say: 

OK 
SAVING 

10 



L 



and begin storing. The VIC cannot check the RECORD key: it can 
only sense that the tape is moving, so be sure to press RECORD. If 
PLAY was already pressed, no message appears > 

When the program has been SAVEd, the VIC will give the 
message: 

READY, 

The VIC has no way of searching for a blank spot on the tape, but 
just records wherever it is, erasing any information that may have 
been there. However, the VERIPr' command can be used to find 
the end of the last program. 

SAVE can be followed by a program name in quotes or in a string 
variable. The VIC will then write the program name before the 
program on the tape, which lets the VIC find it more easily. 

The program can be SAVEd on a device otherlhan the tape deck 
by giving a device number. To do this, put a comma after the 
program name, and then the number of the device. The cassette 
deck is device number 1 , and the disk is number 6. {See examples.) 

It is possible to instruct the VIC to SAVE a program so it will not be 
moved in memory when LOADed. Using command number 1 after 
the device number wilf do this. This is useful when working with 
different memory configurations which may cause VIC memory 
locations to shift. 

To prevent a user from accidentaily trying to read past the last 
information on the tape, the VIC can also write an end-of-tape 
marker after the program. To do this, follow the device number with 
a comma and command number 2. When the VIC finds this marker, 
it will stop and show the message DEVICE NOT PRESENT 
ERROR. 

Command number 3 is a combination of 1 and 2, telling the 
program on tape not to relocate and to put an end-of-tape marker 
after the program. 

SAVE can also be used as a statement within a BASIC program. 
When this statement is hit, the program will be SAVEd normally, 
with the usual prompts appearing on the screen. The program 
resumes normally after the SAVE. 

EXAMPLES 

SAVE Stores program on tape without name 

SAVE "HELLO" Stores on tape with name HELLO 

SAVE AS Stores on tape with name in A$ 

SAVE "HELLO'^.S Stores on device number 8 (disk drive) 

SAVE "HELLO"J .1 Won't relocate HELLO upon re-LOADing 



11 



SAVE "HELL0M.2 Puts an end-oMape marker after the 

program. 
SAVE "HELL0'\1,3 Woni relocate & end-oMape marker 
10 SAVE^'HELLO" Saves the program, then goes on with the 

next program line. 



VERIFY 

Format : Abbreviation : Screen Display: 

VERIFY ■1itename'\ V^^^Ie V S 

device 

This command checks the program on tape or disk against the 
program in the VIC's memory. VERIFY is normally used right after a 
SAVE, to make sure the program was stored correctly on the tape 
or disk, VERIFYing a program after it has been LOADed is useless, 
since the same incorrect program could be in both places. 

When you tell the VIC to VERIFY, this message shows: 

PRESS PLAY ON TAPE 

Once the tape is moving, the VIC checks the program against 
memory, reading until the end of the program. If the copies match, 
the VfC says: 

OK 

READY. 

If there is a problem, you will see this message; 
7VERIFY ERROR 

In this case, you should immediatety SAVE the program on a 
different tape or disk and try again. 

The format of the VERIFY command is similar to the LOAD 
command, A program name can be given either in quotes or a string 
variable, and the VIC will search for that program . If a comma and a 
device number follow the name, the VIC will look at the device 
designated. 

VERIFY is also used to position a tape just past the last program, 
so a new program can be added to the tape without over-writing an 
older one. Just VERIFY with the name of the last program there; the 
VIC searches, checks the program, and stops with a VERIFY 
ERROR. However, your program is still in memory, and now the 
tape is at a btank spot. You can SAVE without worry. 

12 



L 



L 



L 
L 



EXAMPLES: 

VERIFY Checks the first program on tape. 

VERIFY "HELLO" Searches for HELLO, then checks 

VERIFY "HELL0",8 Looks on device 8 for the program. 



[ 



r 

L 



13 



STATEMENTS 

L 



CLR 

DATA 

DEF FN 

DIM 

END 

FOR . . . TO . . . STEP 

GET 

GOSUB 

GOTO or GO TO 

IF . . THEN 

INPUT 

LET 

NEXT 

ON 

POKE 

PRINT 

READ 

REM 

RESTORE 

RETURN 

STOP 

SYS 

WAIT 



r 



f 
[ 



CLR 

Format: Abbreviation: Screen Display: 



CLR C^^^L CU I 

This statement clears out any variables that have been defined, L 

un-DI Mansions any arrays, and RESTORES the DATA pointer 
back to the beginning. This makes available all the RAM memory t 

that the variables had used so that it can be used for something 
different. The RUN command automatically performs the CLR 
operation, as does LOADtng a new program or doing a NEW. Don't 

confuse this statement with the 139 key, which clears the screen. 



14 



EXAMPLE: 

100 A ^ 1014 
110 CLR 
120 PRINT A 

When RUN. this program PRfNTs a zero on the screen. 

DATA 

PQ''"^^^ ' Abbreviation : Screen Display: 

DATA value dI 

I value, , . ■ , value] 

The DATA statement holds Information that will fill variables in a 
READ statement. Any type of information can be stored here, 
separated by commas. If a comma, space, or colon are to be used 
as data, they must be enclosed in quote marks ("). Two commas 
with nothing in between will be read as zero, or an empty string. 
Note: the information in brackets is optional. 

The line containing the DATA statement doesn't actually have to 
be executed while RUNning the program, so most programmers 
leave all the DATA statements at the end of the program, out of the 
way. 

EXAMPLE: 

10 READ A : PRINT A 

20 READ A, B, C : PRINT A; B; C 

30 READ AS : PRINT A$ 

40 FOR L - 1 to 5 : READ A : PRINT A; : NEXT 

50 READ AS, A, 8, B$ : PRINT AS, A, B, BS 

60 END : REM YOU NEVER ACTUALLY HAVE TO HIT THE 

DATA STATEMENTS! 

960 DATA 1 

970 DATA 1,2,3 

980 DATA ABC 

990 DATA 2,3,57,1 i;^HELLO^' -1.2445E-5, 69.7767. ^'A, B, C'^ 

DEF FN 

Format : Abbreviation : Screen Display: 

DEF FN [name] D^^^e D H 

(variable) ^ formula 

15 



When a long mathematical formula is used several times in 
different lines of a program, program memory and typing time can 
be saved by using a defined function for the formula. The function is 
then used throughout the program in place of the lengthy formula. 

The name of the function will be the letters FN and any variable 
name you choose, one or two letters long. The DEF FN statement 
must be executed at least once for the program to use it, so this 
statement is normally placed at the beginning of the program. 

The function name is follov/ed by a variable name inside 
parentheses. Next comes an equal sign, and then the formula. 

Here's an example of a simple formula definition, with an 
example of its use in the program. 

EXAMPLE 1: 



L 



r 

L 



10 DEF FNA(X) - 7 * X 
20 PRINT FNA(1) 
30 PRINT FNA(3) 

The result of line 20 is 7, and the result in line 30 is 21. _ 

EXAMPLE 2: 

10 DEF FNA(X) = [NT(RND(1)*6} + 1 
20 PRINT FNA(IO) 

When the function In this example is used in the program, the 
value of the number in parentheses in line 20 doesn t have any 
effect on the result. This is because in line 1 the variable (X) in the 
parentheses doesn t appear in the formula on the right. ,- 

The next example does use the variable name in the formula. 

EXAMPLE 3: 

10 DEF FNA(X) = INT{RND{irX} + 1 
20 PRINT FNA{10) 

In this case, the number in the parentheses in line 20 does affect 
the result. The number in the parentheses in line 20 is the largest 
random number that will be picked. 

The result of a defined formula must always be a number; there 
are no defined functions for string variables. 



IS 



L 
1 



DIM 



Format: Abbreviation: 


Screen Display: 


DIM variable D^^^f 1 
{ number, . . . , number), 
[variable ( number, . _ , number). 


Dm 



This statement defines an array or matrix of variables, which 
allows you to use the variable name with a subscript. The subscript 
points to the element in the array being used. The lowest element 
number in an array is zero, and the highest is the number given in 
the Dlt^ statement. If an array variable is used without a DIM 
statement to create it, it is automatically DIMensioned to 10 in each 
dimension. 

Let's suppose we wanted to keep track of the score of a football 
game. There are 2 teams, and four quarters plus a possible 
overtime quarter in the game. We could use a matrix to hold the 
scores in each quarter. Here is a program that asks you for the 
score of each team in each quarter: 

EXAMPLE: 

100 DIM S(1,5) , T$(1) 

110 INPUT "TEAM NAMES" ; TS(0), T$(1) 

120 FOR Q = 1 TO 5 

130 FOR T = TO 1 

140 PRINT TS(T). "SCORE IN QUARTER" Q 

150 INPUT S(T,Q) 

160 S(T,0) = S(T, 0) + S{T, Q) 

170 NEXTT,Q 

180 PRINT CHRS(147) "SCOREBOARD" 

190 PRINT "QUARTER"; 

200 FOR Q = 1 TO 5 

210 PRINT TAB{Q*2 +9) Q; 

220 NEXT 

230 PRINT TAB (15) "TOTAL" 

240 FOR T = TO 1 

250 PRINT TS{T); 

260 FOR = 1 TO 5 

270 PRINT TAB (Q*2 +9) S(T, Q); 

280 NEXT 

290 PRINT TAB(15) S(T,0) 

300 NEXT 



17 



This statement will finish the program when RUNning and return 
complete control of the VIC to the person operating it. The CONT 
command can be used to resume execution of the program after 
the END statement was reached, because no variables or pointers 
are cleared. 

The END statement results in the message: READY. 

The difference between STOP and END statements is slight: the 
STOP statement displays the message: 

BREAK IN LINE XXX 

Neither STOP nor END is required to appear at any point in the 
program m VIC BASIC, because a program running out of lines to 
execute will END all by itself. 



IS 



r 



The element numbers in every dimension start at and end at the 
number in the DIM statement. The number of elements created in 
any dimension is the maximum subscript number PLUS 1 . The total 
number of elements is equal to the product of the number of L 

elements in all dimension multiplied together. 

There may be any number of dimensions and any number of 
elements in an array, limited only by the amount of RAM memory 
that is available to hold the variables. The array may be made up of 
normal numeric variables, as shown above, or of strings or integer 
numbers. If the variables are to be other than normal numeric, 
simply use the S or % signs after the variable name to indicate string 
or integer variables. 

It s easy to calculate the amount of memory that will be used up 
by an array: 

MEMORY USED = 5 bytes for variable name 
2 bytes for each dimension 

2 bytes/element for integer variables 
5 bytes/element for normal numeric variables 

3 bytes/element for string variables 
1 byte for each character in each string 
element 

END 

Format: Abbreviation; Screen Display: 

END eBjWBIn B\Z\ 



L 



[I 



FOR , , . TO . ■ . STEP . . , 

Format : Abbreviation : Screen Display: 

FOR variable - F^^^lo F[J 

start TO limit [STEP increment ] 

Tills is a special BASIC statement that lets you easily use a 
variabte as a counter. You must specify certain parameters: the 
variable name, its starting value, the limit of the count, and how 
much to add during each cycle. 

Here is a simple BASIC program that counts from 1 to 10, 
PRINTing each number and ENDing when complete, and using no 
FOR statements: 

100 L = 1 

110 PRINT L 

120 L = L -r 1 

130 IF L <= 10 THEN 110 

140 END 

Using the FOR statement, here is the same program: 

100 FOR L = 1 TO 10 
110 PRINT L 
120 NEXT L 
130 END 

As you can see, the program is shorter and easier to understand 
using the FOR statement. Here is a closer look at the parameters, 
to see how everything works. 

The variable can be any numeric variable name except an array 
variable. When the program reaches a FOR statement, variable is 
set to the value of start. The program proceeds with the statements, 
until a statement containing the word NEXT is reached. 

At that point, increment is added to variable's value. The STEP is 
optional, and if there Is no STEP shown increment is assumed to be 

-rl. 

After increment has been added to variable, the value of variable 
is compared to limit. If the limit has not been exceeded the program 
continues with the line after the FOR statement. If the limit has been 
passed, the line to be executed is the line following the NEXT 
statement. Note: if the STEP value is positive, variable will exceed 
limit when its value is greater than limit, and if the STEP value is 
negative, the variable must be less than limit to end the 
count. The loop will always be executed at least once, re- 
gardless of the values in "start" and 'limit". 

19 



EXAMPLE: 

100 FOR L = 100 TO STEP -1 
100 FOR L = PI TO 6*PI STEP .01 
100 FOR AA = 3 T0 3 

GET 

Format : Abbrevialion : Screen Display: 

GET variable oBSfflME gQ 



L 
L 



L 



This statement lets you input one character at a time from the 
keyboard. Whatever character was hit goes into the variable, tf no 
key was pressed, a zero is placed in a numeric variable, or an 
empty value ("") in a string variable. This differs from the INPUT 
statement in one major respect: If no key is typed, the program 
continues running here, and in the INPUT statement it waits for the 
user to type something. I 

The GET statement is usually placed in a loop to wait for the | 

keystroke. 

EXAMPLE 1: 

10 GET AS: IF AS = '^'^ THEN 10 

The GET can also be used to allow the program to continue 
processing while waiting for data. Example 2 is a simple GET editor 
with a blinking cursor 



EXAMPLE 2: 

10 C = ; Q = 18 

20 GET A$ : G = C + 1 

30 IF C = 10 THEN Q = 164 - Q : C - 

40 PRINT CHRS(Q) CHRS{32} CHRS{146) CHRS(157); 

50 PRINT AS: : GOTO 20 

GOSUB 



r 
[ 



Format : Abbreviation : Screen Display: I 

GOSUB line number GO^^^H S GO ,V 



20 



L 



This is a specialized form of the GOTO statement, with the 
important difference that GOSUB remembers where it came from. 
When the RETURN statement (different from the RETURN key on 
the l<eyboard) is reached in the program, the program jumps back 
to the statement immediately following the original GOSUB 
statement from which it came. 

The major use of a subroutine {GOSUB really means GO to a 
SUBroutine) is when there is a small section of program that is used 
by different sections of the program. By using subroutines rather 
than repeating the same lines over and over at different places En 
the program, you can save lots of program space. In this way, 
GOSUB is similar in use to DEF FN: DEF FN lets you save space 
when using a formula, and GOSUB saves space when using a 
several-line routine. 

Here is an inefficient program that doesn't use GOSUB: 

100 PRINT ^THIS PROGRAM PRINTS" 

110 FOR L = 1 TO 500 : NEXT 

120 PRINT 'SLOWLY ON THE SCREEN^' 

130 FOR L = 1 TO 500 : NEXT 

140 PRINT "USING A SIMPLE LOOP" 

150 FOR L = 1 TO50O : NEXT 

160 PRINT "AS A TIME DELAY." 

170 FOR L = 1 TO500 : NEXT 

Here is the same program using GOSUB: 

100 PRINT 'THIS PROGRAM PRINTS" 

110 GOSUB 20O 

120 PRINT ^'SLOWLY ON THE SCREEN" 

130 GOSUB 200 

140 PRINT "USING A SIMPLE LOOP" 

150 GOSUB 200 

160 PRINT "AS A TIME DELAY." 

170 GOSUB 200 

180 END 

200 FOR L = 1 TO 500 : NEXT 

210 RETURN 

Each time the program executes a GOSUB, the line number and 
position in the program line are saved in a special area called the 
"stack," which takes up 256 bytes of your memory. This limits the 
amount of data that can be stored in the stack. Therefore, the 
numberofsubroutinereturnaddressesthatcan be stored is limited, 
and care should be taken to make sure every GOSUB hits the 
corresponding RETURN, or else you'il run out of memory even 
though you have plenty of bytes free. 

21 



GOTO or GO TO 



Format: Abbreviation : Screen Display: 

GOTO line number G^^fflHo G □ 



This simple statement allows the BASIC program to execute 
lines out ot numerical order. The word GOTO followed by a number 
will make the program jump to the line with that number. GOTO 
cannot be followed by a variable, but must have the line number 
typed after the word GOTO, 

EXAMPLE 1: 

10 GOTO 10 

Notice that the loop in the example never ends, since the 
program keeps running the same line over and over. This is called 
an "infinite loop/' and can be utilized when you want a program to 
stop in place and wait. The only way to stop an infinite loop ts with 

EXAMPLE 2: 

10 PRINT "^HELLO"; 
20 GOTO 10 

IF . . . THEN 

Format Choices : Abtareviation : Screen Display: 

IF expression THEN line number None None 

IF expression THEN statement 



This is the statement that gives BASIC most of its "intelligence;' 
the ability to evaluate conditions and take different actions 
depending on the outcome. 

The word IF is followed by an expression, which can include 
variables, strings, numbers, comparisons, and logical operators. 
The word THEN is followed on the same line by either a tine number 
or one or more BASIC statements. When the expression is false, 
everything after the word THEN on that line is ignored, and 

22 



L 

r 



L 



execution continues with the next line number in the program. A 
true result makes the program either branch to the line number after 
the word THEN or execute whatever other BASIC statements are 
found on that line. 

EXAMPLE 1 : 

100 INPUT "TYPE A NUMBER"; N 

110 IF N <= OTHEN 200 

120 PRINT "SQUARE RO0T=" SQR(N) 

130 GOTO 100 

200 PRINT "NUMBER MUST BE >0" 

210 GOTO 100 

This program prints out the square root of any positive number. 
The IF statement here is used to validate the result of the INPUT. 
When the result of N < = is true, the program skips to fine 200, 
and when the result is false the next line to be executed is 1 20. Note 
that GOTO is not needed with IF . . . THEN, as in line 110 where 
THEN 200 actually means THEN GOTO 200. 

EXAMPLE 2: 

100 FOR L = 1 TO 100 

110 IF RND(1) < .5 THEN X = X + 1 : GOTO 130 

120 Y = Y + 1 

130 NEXT L 

140 PRINT "HEADS = " X 

150 PRINT "TAILS = " Y 

The IF in line 1 20 tests a random number to see if it is less than .5. 
When the result is true, the whole series of statements following the 
word THEN is executed: first X is incremented by 1, then the 
program skips to line 130. When the result is false, the program 
drops to the next statement, line 120. 

EXAMPLE 3: 

100 PRINT CHRS(1 47); 

110 FOR X = 1 TO 23 

120 FOR Y = 1 TO 22 

130 IFX = 23ANDY = 22THENPRINTCHR$(157)CHR$(148); 

140 PRINT "Z"; 

150 NEXT:NEXT 

160 GOTO 160 



2i 



EXAMPLE 1 : 

100 INPUT A 

110 INPUT B, C, D 

120 INPUT "PROMPT" 



24 



r 



This program will fill the entire screen with Z s, including the 
bottom right corner, and then freeze. The IF in tine 120 checks for 
both X = 23 and Y = 22 being true, or else the program just drops 
through to line 1 30. When the conditions are true, the VIC PRINTS a L 

cursor left and an insert. 

By the way, this really is a trick to PRINT in the lower right corner j 

in the screen without forcing the screen to scroll up a line. This is 1 

because you never really PRINT in that position, you insert in the 
position before that, which pushes the character into position, i 

For those of you using cartridges that add extra commands to 
BASIC, like "Super Expander" and Programmers Aid;' make sure 
that you put a colon between the word THEN and one of the extra 
commands. 



EXAWIPLE 4: 






100 IFX = 4THEN : GRAPHIC 4 






INPUT 




p 


Format: Abbreviation: 


Screen Display: 


INPUT r'promptM None 


None 




variable 







This is a statement that lets the person running the program i 

"feed'' information into the computer. When executed, this 
statement PR INTs a question mark (?) on the screen, and positions 
the cursor 2 spaces to the right of the question mark. Now the 
computer waits, cursor blinking, for the operator to type in the [ 

answer and press the RETURN key, L 

The word INPUT may be followed by any text contained in quote 
marks ("). This text is PRINTed on the screen, followed by the [ 

question mark. | 

After the text comes the name of one or more variables 
separated by commas. This variable is where the computer stores . 

the information that the operator types. The variable can be any 
legal variable name, and you can have several different variable ' 

names, each for a different input. 

L 



L 



When this program runs, the question mark appears to prompt 
the operator that the vrC is expecting an input for line 100. Any 
number typed in goes into A, for later use rn the program. If the 
answer typed was not a number, the ?REDO FROM START 
message appears, which means that a string was received when a 
number was expected. If the operator just hit RETURN without 
typing anything, the variable's value doesn't change. 

Now the next question mark, tor line 1 10, appears. If we type only 
one number and hit RETURN, the VIC will now display 2 question 
marks (??), which means that more input is required. You can just 
type as many inputs as you need separated by commas, which 
prevents the double question mark from appearing. If you type 
more data than the INPUT statement requested, the ?EXTRA 
IGNORED message appears, which means that the extra items 
you typed were not put into any vahables. 

Line 1 20 displays the word PROMPT before the question mark 
appears. The semicolon is required between the prompt and any 
list of variables. Note: The only way to end a program during 
an INPUT statement is to hold down the RUN/STOP key and 
hit RESTORE. 

EXAMPLE 2: 

10 PRINT "INPUT A WORD^'JNPUT A$ 
20 PRINT ^'YOUR INPUT WAS"AS 
30 GOTO 10 

LET 

Format : Abbreviation : Screen Display: 

[LET] variable = L^^Me L f— : 

expression 

The LET statement is used to set a variable to a value. The value 
can be a constant (like 5) another variable (like C), or a complex 
formula {like PI*R-3). The LET statement also works with string 
variables. 

Since the LET statement is used so often in BASIC programs, the 
word LET' has been made optional, since it did nothing but take up 
memory. Advanced programmers always leave it out. 

LET B = 1 This is the same as B = 1 

A = 3*3'33-B A will equal 41 

AS = XAT ■ ^ "DOG" AS will equal XATDOG" 

m 



NEXT 

Format: Abbreviation : Screen Display: 

NEXT [variable , variable N^^J E N Q 

, . . . , variable] 

This statement completes a loop that was started by a FOR 
statement. If the word NEXT is not followed by a variable name, the 
ioop completed is the last one that was started. If there is a variable 
name given, that loop is finished. If the loop being finished wasn1 
the last one started, any loops between the last one and the one 
specified in the NEXT statement are lost. 

Care must be taken when placing loops within other loops, to 
complete them properly. Here is an example of correct placement 
("nesting') of loops. 

EXAMPLE 1: 

10 FOR L = 1 TO 100 
20 FOR M = 1 TO 10 
30 NEXT M 
40 NEXT L 

Notice that the first loop finished is the last one that was started. 
Here are some general examples of the NEXT statement. 

EXAMPLE 2: 

NEXT 

NEXT J 
NEXT I, J, K 

ON 

Formats : Abbreviation : Screen Dlspiay: 

ON variable GOTO number [, number , . . . , number] 
ON variable GOSUB number I number number] 



This statement allows the program to choose from a list of line 
numbers to go to. If the variable has a value of 1, the first line 
number is the one chosen. If the value is 2, the second number in 
the list Is used, and so on. If the value in the variable is less than 1 or 
greater than the number of line numbers in the list, the program just 

26 



r 

L 



ignores the statement and continues with the statement following 
the ON statement, 

EXAMPLE 1: 

ON X GOTO 100, 130, 180, 220 
ON X + 3 GOSUB 9000,20,9000 

ON is really an under-used variant of the IF . . . THEN . . . 
statement, which can send the program to one of many possible 
lines. Using formulas and logical operators, one ON statement can 
replace a whole list of IF statements. 

EXAMPLE 2: IF statements 

IF A = 7 THEN 400 
IF A = 3 THEN 900 
IF A < 3 THEN 1000 
IF A > 7 THEN 100 

EXAMPLE 3: ON . . . GOTO . . . 

ON -(A -7) -2* (A = 3) -3*(A<3) -4* (A>7) GOTO 400, 900, 
1000, 100^ 




POKE 

Format: Abbreviation : Screen Display: 

POKE location, value pB!nWo P □ 



This statement allows you to alter the value of any RAM [ocation 
in memory. There are a possible 65,536 locations in the VIC's 
memory, and in an unexpanded VIC a little more than 5K of them 
are RAM and can be changed. Your 5K of RAM is in locations 
numbered from to 1023 and from 4096 to 8191. The memory 
maps in Chapter 3 describe the contents of the first 1 K. Your BASIC 
program, variables, and the screen memory all go in the 4K area, 

27 



Color RAM is an extra half-K block of memory starting at 38400. 
There are also alterable areas in some of the chips, like the VIC chip 
from 36864 to 36879 and the 6522 chips above that. 

Because each memory location holds 1 byte, which can have a 
value from to 255, only numbers in that range can be POKEd into 
memory. 

EXAMPLE: 

POKE 36879 , 8 
POKE A , B 

PRINT Abbreviation : ? 

There is no statement in BASIC with more variety than the PRINT 
statement. There are so many symbols, functions, and parameters 
associated with this statement that it might almost be considered as 
a language of its own within BASICp a language specially designed 
for writing on the screen. 

Quote mode 

Once the quote mark (SH IFT 2) is typed, the cursor controls stop 
operating and start displaying reversed characters which actually 
stand for the cursor control you are hitting. This allows you to 
program these cursor controls, because once the text inside the 
quotes is PRINTed they perform their functions. The DEL key is the 
only cursor control not affected by "quote mode." 

1* Cursor movement 

The cursor controls which can be "programmed" In quote mode 
are: 

Key Appears as 



11 

CE 

m 



D r 



28 



If you wanted the word HELLO to PRINT diagonally from the 
upper feft corner of the screen, you would type: 



PRINT" im H ^m E Rffl L Effl L Effl 0" 

or 

PRINT" ^H[Q]E[§L[gL5]0'^ 



2. Reverse charac ters 

Holding down the^^Jkey and hitting^^^will cause a [r] 
to appear inside the quotes. This will make al! characters start 
printing in reverse vi deo (like a negative of a picture). To end the 
reverse prrntingjnit ^^J Jtj^ , which prints a^Zjor else 
PFIINT a^^^ffl. (Just ending the PRINT statement without a 
semicolon or comma will take care of this.) 

3. Color controls 

Holding down the CTRL key with any of the 8 color keys will make 
a special reversed character appear in the quotes. When the 
character is PRINTed, then the color change will occur. 

Colo r Appears as 

a 



ifflk B 



B 





ff you wanted to PRINT the word HELLO in cyan and the word 
THERE in green, type: 



PRINT " fJOg ^Q HELLO KEM MW^ THERE" 

or 

PRINT '■ ^ HELLO jTj THERE" 

29 



4. Insert mode ^^ 

The spaces created by using the insert |l||| key have some of 

the same characteristics as quote mode. The cursor controls and 

color co ntrols show up as reversed characters. The only difference 

is in the WJM and ^9 ||B, which performs its normal function 

even in quote mode, now creates the T^ . And ||j|l, which created 

a special character in quote mode, inserts spaces normaEly. 

Because of this, it is possible to create a PRINT statement 
containing DELetes, which cannot be PRINTed in quote mode. 
Here is an example of how this is done: 



10 PRINrHELLO 
which displays as 
10 PRINT"HELLO[T][T]P" 

When the above line is RUN, the word displayed wilt be HELP, 
because the last two letters are deleted and the P is put in their 
place. 

WARNING: The DELetes will work when LISTing as well as 
PRINTing, so editing a line with these characters will be difficult. 

The 'Insert mode'' condition is ended when the RETURM (or 
SHIFT RETURN) key is hit, or when as many characters have been 
typed as spaces were inserted. 

5. Other special characters 

There are some other characters that can be PRINTed for 
special functions, although they are not easily available from the 
keyboard. In order to get these into quotes, you must leave empty 
spaces for them in the line, hit RETU RN or SHI FT RETURN, and go 
back to the spaces with the cursor controls. Now you must hit CTRL 
RVS ON, to start typing reversed characters, and type the keys 
shown below: 

Function Type 



SHIFT RETURN 


SHIFT M 


switch to lower case 


N 


switch to upper case 


SHIFT N 


disable case-switching keys 


H 


enable case-switching keys 


1 



The SHIFT RETURN will work in the LISTing as well as 
PRINTing, so editing will be almost impossible if this character is 
used. The LISTing will also look very strange. 

30 



L 



READ 

Format : Abbreviation : Screen Display: 

READ variable list R ^^^M E R g 

This worlds with the DATA statement to fili variables with values 
stored within the program. The information is usually in the form of a 
list that is READ in at the beginning of the program, or a table that is 
re-READ during the program. READ works just like INPUT, except 
that the information comes from DATA statements instead of the 
person working the program, 

EXAMPLE: 

10 READ A, B, CS 

20 DATA 1, 2, HELLO THERE! 

REM 

Format : Abbreviation : Screen Display: 

REM any text None None 



This statement makes your program more easily understood 
when LISTed. It is a reminder to yourself to tell you what you had in 
mind when writing each section. For instance, you might tell what a 
variable is used for, what date the program was written,, or some 
other useful piece of information. The REMark can be any,text, 
word, or character, including the colon {:) or BASIC keywords! 
Therefore, the REM statement is the last one on a line that the 
program sees. 

If you try to use graphic characters in a REM statement without 
using a quote mark {") first, when you LfST the line you'll see BASIC 
keywords instead of the graphic characters. This is because the 
VIC thinks these characters are the "'tokens" for those commands. 
The BASIC tokens are discussed in the last part of this chapter. 

EXAMPLE; 

REM PROGRAM BY SUE M. 10/6/81 Good example 
REM AS HOLDS 22 CURSOR DOWNS Good example 
LET A- 1 : REM PUT A 1 IN A Bad example 

31 



RESTORE 

Format : Abbreviation : Screen Display: 

RESTORE ReB!!BMs RE [v] 



This statement completes a subroutine thiat was begun witii tlie 
GOSUB statement. When the GOSUB is performed, the VIC 
remembers which line it came from. When it later hits a RETURN 
statement, it goes back to the statement right after the original 
GOSUB. This is similar to a GOTO, except the GOSUB subroutine 
can be performed and the program continued from the original 
GOSUB line. 

EXAMPLE: 

10 PRINT "THIS IS THE PROGRAM" 

20 GOSUB 1000 

30 PRINT "PROGRAM CONTINUES" 

32 



r. 



r 



This statement sets the DATA statement pointer back to the first 
DATA statement in the program. Each time you READ the DATA, 
the pointer advances through all the items in the first DATA 
statement, then through the items in the next DATA statement, and 
so on through all the DATA statements in the program. In order to 
re-READ the items, use the RESTORE statement. 

EXAMPLE: 

10 DATA 1, 2, 3, 4 

20 DATA 5, 6, 7, 8 

30 FOR L = 1 TO 8 

40 READ A : PRINT A i 

50 NEXT 

60 RESTORE ' 

70 FOR L=1 TO 8 

80 READ A : PRINT A I 

90 NEXT I 

RETURN L 

Format : Abbreviation : Screen Display: 

RETURN RE^^Jt RE [□ f 



L 



r 

L 



40 GOSUB 1000 




50 PRINT -MORE PROGRAM" 




60 END 




• 1000 PRINT THIS IS THE GOSUB" 


RETURN 


STOP 




^ Format: Abbreviation: 


Screen Display: 


STOP SgJ^T 


S 



This statement will hait a program and return controi to the user. 
The only difference between the STOP and END statements is that 
the message BREAK IN LINE XXXX appears when STOP is used, 
just as if the user had pressed the G|Q Key, 

EXAMPLE: 
100 STOP 

SYS 

Format : Abbreviation : Screen Display: 

SYS location sKflfSlY S [_J| 



This is the most common way to mix a BASIC program with a 
machine language program. The machine language program 
begins at the location given in the SYS statement. When the 
machine language instruction RTS (return from subroutine) is 
reached, the program jumps back to the BASIC program, right after 
the SYS statement. A machine language program can be POKEd 
into memory from BASIC or created with the aid of ViCMON^". 

EXAMPLE: 

SYS 64802 resets the VIC from power-up 

POKE 4400 , 96 : SYS 4400 returns immediately 



33 



WAIT 




Format: Abbreviation: 

WAIT location, W^^^A 
mask1[, mask2] 


Screen Display: 

W ♦ 



For most programmers, this statement sliouid never be used. It 
causes tlie program to hait until a specific memory location's bits 
change in a specified way. This is used for arcane I/O operations 
and almost nothing else. 

The WAIT statement takes the value in the memory location and 
performs a logical AND operation with the value in mask1 , If there is 
a mask2 in the statement, the result of the first operation is 
exclusive-ORed with mask2. This sounds confusing, but there's an 
easier way to look at it. The maski value "filters out*' any bits that 
we don^t want to test. Where the bit is in maski , the corresponding 
bit in the result will always be 0. The mask2 value will flip any bits, so 
we can test for an off condition as well as on. Any bits being tested 
for a should have a 1 in the corresponding position in mask2. 

EXAMPLE: 

WAIT 36868 , 144 , 16 

What are we testing for here? Here's a binary look at our two 
masks: 

144 ^ 10010000 
16 = 00010000 

This WAIT statement will halt the program until etthsrthe 128 bit 
is on or the 32 bit is off. 



L 



34 



I/O STATEMENTS 



CLOSE 
CMD 

QET# 
INPUT* 
OPEN 
PRINT* 



CLOSE 

Format: Abbreviation : Screen Display: 

CLOSE file* CL K!!!M O CL □ 



This cioses tiie file that was started in an OPEN statement. It is 
recommended that a PRINT* to that file be performed before 
closing the file, to make sure that all data has been transmitted. Not 
closing an OPEN file results in a FILE OPEN ERROR. 

EXAMPLE: 

OPEN 1,4 : PRINT#1 . "HI THERE!" : CLOSE 1 

CMD 

Format: Abbreviation : Screen Display: 

CMDfjIe* cBSniMM CJSj 



This changes the normal output device of the VIC from the 
screen to the file specified. In this way, data and LISTings can be 
sent to other devices, like the printer, disk, or tape drive. When 
finished transmitting, to reset output to the screen, do a PRINT* 
and CLOSE the file. 

EXAMPLE: 

OPEN 1,4: CMD 1 : PRINT "HELLO THERE!" :PRINT#1 ; CLOSE 1 

35 



GET# 




Format: 


Abbreviation: 


GET# fite#, 


None 


variable 





Screen Display: 



None 



This statement receives data one byte at a time from any 
OPENed device. If no data is available, it works the same as the 
GET statement, returning a null value. The INPUT# statement can 
get more than one character, and will get all the characters up to a 
carriage return (CHR$(1 3)), The GET# will receive any characters. 
1 at a time, including special characters like the carriage return and 
quote marks. 

EXAMPLE: 

10 OPEN 1, 3 

20 PRINT CHRS{147) "HELLO THERE" CHRS{19); 

30 FOR L = 1 TO 22 

40 GET#1, 8S : A$ = A$ + B$ 

50 NEXT : PRINT A$ : CLOSE 1 

If you examine AS when this program is finished, youll see that 
the last character is a CHRS(13), a carriage return, which 
terminates the line. 



INPUT# 

Format : Abbreviation : Screen Display: 

iNPUT# file#> iI^^Jn I \Z\ 

variabJe 1[, variable 2, etc.] 

This usually is the fastest and easiest way to retrieve data that 
was stored in a file on tape or disk. The data is in the form of whole 
variables, as opposed to the one-byte-at-a-time method of GET#. 
First the file must have been OPENed, then you can use IN PUT# to 
fill your variables. 

EXAMPLE: 

10 0PEN1. 1. 0, 'TAPE FILE NAME'' 
20 PRINT "FILE IS OPEN OK" 
30 INPUT# 1, AS. B$ 
40 CLOSE 1 

36 



L 

r 



L 



r 



When using the screen (device # 3) as an input device, the 
INPUT# statennent can be used to read any whole line of text 
from the screen. The last character of the line will be read as 
a CHR$(13), as if the screen hit the RETURN key at the end 
of the line! 

However, there are times when it's not always practical to use 
INPUT#, and some precautions are m order. If a string variable put 
on the file has certain characters, using !NPUT# could have 
unexpected results. If you use CHRS(13), or a comma (,) or 
semicolon {:) or colon {:) , the VIC will think that this marks the end of 
your variable. If you put quote marks (CHRS(34}) at the start and 
end of your sthng when it [s written, it will come back intact. 

I N PUT # may also be used to ^1NPUT" data without the question 
mark (?) prompt being displayed. This is very useful for a variety of 
applications, for example if you want to set up a graphic chart and 
let the operator INPUT data to the chart without question marks 
being displayed. 

EXAMPLE: 

10 OPEN 1,0 

20 PRINT "'ENTER A NUMBER": INPUT#1 , A 

30 PRINT A ^TIMES" 5 "EQUALS" A*5 



OPEN 

Format : Abbreviation : Screen Display: 

opENfiie#, oKffnaBp on 



[device#, command#, string] 

This statement OPENs a channel for input and/or output to a 
device. This device can be part of the VIC, like the screen and 
keyboard, or an accessory, [ike the tape recorder, printer, or disk 
drive. When OPENlng a channel to an external device, the VIC sets 
up a buffer for the data, and only transmits and receives whole 
buffers at a time. 

The file# can be any number from 1 to 255t and is the same 
number that will be used In the INPUT#, GET#, and PRINT# 
statements to work with this device. The device# specifies which 
device, and is set within that device. 



37 



DEVICE* 


DEVICE 





keyboard 


1 


cassette deck 


2 


RS232 device 


3 


screen 


A 


printer 


5 


printer 


8 


disk drive 


4-127 


serial bus device 


128-255 


serial bus device— send If after cr 



The command* is specific to each different device. Here are 
some of the command numbers: 



DEVICE 


COMMAND# 


EFFECT 


Cassette 





read tape file 




1 


write tape file 




2 


write tape file, put EOT marker at 
end 


Disk 


1-14 


open data channel 




15 


open command channel 


Keyboard 


1-255 


no effect 


Screen 


1-255 


no effect 


Printer 





upper case/graphics 




7 


upper/lower case 


RS232 




See RS232 (Section 4) 



The string at the end is sent to the printer or screen as If a 
PRINT# were performed to that device. With the cassette deck, it is 
used for the filename, and with disk it can be a filename or some 
control information. 



EXAMPLE: 

OPEN 1. 

OPEN 1.1.0, "name" 



Read the keyboard 
Read from cassette 

38 



r 

L 



OPEN 1,1,1, "name" Write to cassette 

OPEN 1p 1, 2, "name" Write to tape, put EOT marker 

after file 
OPEN 1, 2, 0, "string" Open channel to RS232 device 

OPEN 1, 3 Read/write screen 

OPEN 1, 4, 0, 'string'" Send upper case/grapfiics to 

printer 
OPEN 1, 4, 7, "string'* Send upper/lowercase to printer 

OPEN 1, 5, 0, "string" Send upper/lower case to printer, 

device# switcfied 
OPEN 1,8, 15, 'command" Send command to disk 

PRINT# 

Format: Abbreviation : Screen Display: 

PRINT# fiie#. P^^^R P Q 

variable list 



Tfiis sends tfie contents of the variables in the list to the device 
that was previously OPENed. They will be transmitted in the same 
format as if PRINTed to the screen; if commas are used as 
separators extra spaces will appear, if semicolons are used no 
spacewiliappear, andaCHR$(13) is the last character sent tf there 
isn t a comma or semicolon at the end of the line. The easiest way to 
whte more than one variable to a file on tape or disk is to set a string 
variable to CHRS(13), and use that string in between ail the other 
variables when writing the file. 

EXAMPLE; 

100 OPEN 1,1,1, "TAPE FILE" 

110 R$ = CHRS{13) 

120 PRINT#1. 1; R$; 2; R$; 3; R$; 4; R$; 5 

130 PRINT#1. 6 

140 PRINT#1, 7 

The example shows how to write a tape file that can be easily 
read back using iNPUT# statements, since each variable has a 
CHR$(13) printed after it. You can also print '\" or ";" to separate 
the variables. 



39 



VIC 20 BASIC FUNCTIONS 



r 



The intrinsic functions provided by BASIC are presented in the 
following paragraphs. The functions may be called from any 
program without further definition. 

Arguments to functions are always enclosed in parentheses. In 
the formats given for the functions in this chapter, the arguments 
have been abbreviated as follows : 

X and Y Represent any numeric expression 

I and J Represent integer expressions 

XS and YS Represent string expressions 

If a floating point value is supplied where an integer is required, 
BASIC wiEl round the fractional portion and use the resulting 
integer. 

ViC 20 BASIC Functions 



[ 



FUNCTION 


Numeric 


RESULT 

String 


ABS 


X 


ASC 


X 


ATN 


X 


CHRS 


X 


COS 


X 


EXP 


X 


PRE 


X 


INT 


X 


LEFTS 


X 


LEN 


X 


LOG 


X 


MID$ 


X 


PEEK 


X 


POS 


X 


RIGHTS 


X 


RND 


X 



r 



40 



SGN 


X 




SIN 


X 


SPC 


X 


SQR 


X 


STATUS 


X 


STRS 


X 


TAB 


X 


TAN 


X 


TIME 


X 


TIMES 


X 


USR j 


X 


VAL 


X 



ABS 

Format : 

ABS(X) 



Abbrevfatfon: 



B 



Screen Display: 

Arc 



Action: Returns the absolute value of the expression X. 

EXAMPLE: 

PRINT ABS {7*(-5)) 

35 

READY. 



ASC 

Format : 

ASC(XS) 



Abbreviation: 



Screen Display: 



Action: Returns a numerical value that is the ASCII code of the first 
character of the string XS. (See Appendix F for ASCII codes.) If XS 
is null, an "ILLEGAL QUANTITY'' error is returned. 



41 



EXAMPLE: 

10 XS = "TEST" 
20 PRINT ASC{XS) 
RUN 
84 
READY. 

See the CHRS function for ASCM-to-string conversion. 

ATN 

Format: Abbreviation: S creen Display: 

ATN(X) A BHMt a O 



r 
[ 



Action: Returns the arctangent of X in radians. Result is in the 
range - pi/2 to pi/2 . The expression X may be any numeric type, but 
the evaluation of ATN is always performed in floating point binary. 

EXAMPLE: 

10 INPUT X 
20 PRINT ATN(X) 
RUN 
?3 
1 .24904577 
READY. 

CHR$ 

Forma! : Abbreviation; Screen Display; 

CHRS(I) cBBBIh cQ 



Action: Returns a siring wfiose one element has ASCII code I. 
(ASCII codes are listed in Appendix F.) CHR$ is commonly used to 
send a special character to the terminal. For instance, a screen 
clear could be sent (CH R$(1 47)} to clear the CRT screen and return 
the cursor to the home position, as a preface to an error message. 

EXAMPLE: 

PRINT CHRS(66) 

B 

READY. 

42 



r 

L 



See the ASC function for ASCII-to-numeric conversion. 

COS 

Format; Abbreviation : Screen Display: 

COS(X) None None 



Action: Returns the cosine of X in radians. The calculation of 
COS(X} is performed in floating point binary. 

EXAMPLE: 

10X=2*COS(.4) 
20 PRINT X 
RUN 

1.84212199 
READY. 

EXP 

Fornriat: Abbreviation ; Screen Display; 

EXP(X} eHSBIx E \E 



Action: Returns 


e to the power of X. X must be < = 88.029691 91 . If 


EXP overflows. 


the 


"OVERFLOW" error message is displayed. 


, EXAMPLE: 

[ 10X = 5 










20 PRINT EXP 


{X- 


-1) 


[ RUN 






54.5981501 






' READY. 






FRE 






Format: 




Abbreviation: Screen Display: 


FRE(X} 




fKIBBBr f^ 



Action: Arguments to FRE are dunnmy arguments. FRE returns the 
number of bytes in memory not being used by BASIC, 



43 



EXAMPLE: 

PRINT FRE(O) 

14542 
READY. 



INT 

Format: 

INT(X) 



Abbreviation : 

None 



Action; Returns the largest integer <=X. 

EXAMPLE: 

PRINT INT(99.4343), INT(- 12.34) 

99 -13 

Ready. 



LEFTS 

Format : 

LEFTS(XS, I) 



Abbreviation: 



LE 



Screen Display: 

None 



L 
L 



[ 



Screen Display: 

leQ 



Action: Returns a string comprised of the leftmost I characters of 
XS. I must be in the range to 255. If I is greater than LEN(X$), the 
entire string (XS) will be returned. If I = 0. the null string (length zero) 

is returned. 

EXAMPLE: 

10 AS = "COMMODORE COMPUTER" 

20 B$ = LEFTS(AS. 9) 

30 PRINT BS 

COMMODORE 

READY. 

Also see the MIDS and RIGHTS functions. 



44 



[ 

L 



r 

L 



LEN 

Format; Abbreviation : Screen Display: 

LEN(X$) None None 



Action: Returns the number of characters in XS. Non-printing 
characters and blanks are counted. 

EXAMPLE: 

10 X$ = "COMMODORE COMPUTER" 
20 PRINT LEN (X$) 

18 
READY. 



LOG 

Format: Abbreviation : Screen Display: 

LOG(X) None None 



Action: Returns the natural logarithm of X. X must be greater than 
zero. 

EXAMPLE: 

PRINT LOG (45/7) 

1 .86075234 
READY. 



MID$ 

Format : Abbreviation : Screen Display: 

MID$(XSJ[,J]) MlSflBlf mQ 



Action: Returns a string of length J characters from XS beginning 
with the ith character. I and J must be in the range to 255. If J is 
omitted or if there are fewer than J characters to the right of the Ith 
character, all rightmost characters beginning with the Ith character 
are returned. If f>LEN(X$), MIDS returns a null string. 



45 



EXAMPLE: 

LIST 

10 A$ = "GOOD" 

20 B$= "MORNING EVENING AFTERNOON" 

30 PRINT A$;MIDSCB$,9,7) 

RUN 

GOOD EVENING 

READY. 

Also see the LEFTS and RIGHTS functions. 



PEEK 

Format : Abbreviation : Screen Display: 

PEEK(i) pK!!B1e pg 



Action: Returns the byte (decinnal integer in the range to 255) 
read from memory location I. I must be in the range to 65535. 
PEEK is the complementary function to the POKE statement. 

EXAMPLE: 

PRINT PEEK{36879) 

This will return the value of the screen background color byte. 

POS 

Format : Abbreviation : Screen Display: 

POS(X) None None 



Action: Returns the current cursor position. The leftmost position is 
0. X is a dummy argument. 

EXAMPLE: 

IF POS(X) >20 THEN PRINT CHRS{13) 



46 



L 



r 
[ 



r 



RIGHT$ 

PofrnaV. Abbreviation : Screen Display : 

RIGHTS{XS, t) R^^a I R Q 



Action: Returns tiie rightmost I characters of string XS. If 
l = LEN(XS). returns X$. If 1 = 0, the null string (length zero) is 
returned. 

EXAMPLE: 

10 AS = "COMMODORE BUSINESS MACHINES" 

20 PRINT RIGHTS(ASf 8) 

RUN 

MACHINES 

READY. 

Also see the MIDS and LEFTS functions. 
RND 

^"''"^a*- Abbreviation : Screen Display: 

RND(X) R^^An R 



Action: Returns a random number between and 1 . X>0 returns 
the same pseudo-random number sequence for any random 
number seed. X<0 reseeds the sequence, each X producing a 
different seed. The sequence is seeded at random on power-up. 
X=0 generates a random number from a free running clock. 

EXAMPLE: 

10 FOR 1 = 1 TO 5 
20 PRINT INT{RND(X)*100); 
30 NEXT 
RUN 

24 30 31 51 5 
READY. 



47 



SGN 



Format: 



Abbreviation : Screen Display: j 



SGN(X) sBflalG SO 



Action: IF X>0, SGN(X) returns 1. If X-0, SGN(X) returns 0. If 
X<0, SGN(X) returns -1. 

EXAMPLE: 

ON SGN(X)^2 GOTO 100, 200, 300 100 if X is negative, 

200 if X is and 



300 if X is positive- 



SIN 



Format : Abbreviation : Screen Display: 

siN(X) sKffinii Shk] 



Action: Returns the sine of X in radians. SIM{X) is calculated in 
floating point binary. COS(X) = S!N(X4-3.14159265'2) 

EXAMPLE: 

PRINT SIN (1.5) 

.997494987 
READY. 



SPC 

Format : Abbreviation : Screen Display: 

spc(i) sB3!!Mp s^ 



Action: Prints I blanks on the screen. SPC may only be used 
w/ith PRINT. I must be in the range to 255. 

EXAMPLE: 

PRINT "OVER" SPC(15) THERE" 
OVER THERE 

READY 

48 



L 



SQR 

Format: 

SQR(X) 



Abbreviation: 



Q 



Screen Dispiay: 



Action: Returns the square root of X. X must be >=0. 

EXAIVIPLE: 

10 FOR X = 10 TO 25 STEP 5 

20 PRINT X, SQR(X) 
30 NEXT 
RUN 

10 3.16227766 

15 3.87298335 

20 4.47213595 

25 5 
READY. 



STATUS 

Format: 

STATUS 



Abbreviation : 
ST 



Screen Display: 
ST 



Action: Returns the CBM status corresponding to the last I/O 
operation, whether over cassette, screen, keyboard or serial bus. 



ST 
bit 
position 


ST 

numeric 

value 


Cassette 
Read 


Serial Bus 
R/W 


Tape 
verify 
-f load 





1 




tinne out 
write 




1 


2 




time out 
read 




2 


4 


short block 




short block 


3 


8 


long bfock 




long bfock 


4 


16 1 

1 
\ 


unrecover- 
able read 
error 




any mismatch 



49 



ST 


ST 


Cassette 


Serial Bus 


Tape 


bit 


numeric 


Read 


RAW 


verify 


position 


value 






+ load 


5 


32 


checksum 
error 




checksum 
error 


6 


1 64 


end of file 


EOl 




7 


-128 


end ot tape 


device not 
present 


end of tape 



L 
L 

r 



EXAMPLE: 

10 OPEN 2,1,2, "MASTER FILE" 

12 GET#2,AS 

14 IF STATUS AND 64 THEN 20 

16 ?A$ 

18 G0T012 

20 ?A$: CL0SE2 



r 



STR$ 

Format : 

STR$(X) 



Abbreviation: 



ST 



Screen Display: 
STQ 



Action: Returns a string representation of value of X. 

EXAMPLE: 

5 REM LINE UP DECIMAL POINTS 

10 INPUT "TYPE A NUMBER";N 

20 A$ = STRS(N); Q = LEN(AS) 

30 FOR L = QTO 1 STEP -1 

40 IF MIDS(AS, L, 1) <> "." THEN NEXT: AS = AS + ".00" 

GOTO 60 
50 IF L = Q - 1 THEN AS = AS + "0" 
60 PRINT TAB(10)A$ 
70 GOTO 10 
Also see the VAL function. 



r 



50 



r 



TAB 

Format: 

TAB(I) 



Abbreviation: 



Screen Display: 



Action: Spaces to position I on the screen. If the current 
print position is already beyond space I TAB goes to that 
position on the next line. Space ts the leftmost position, 
and the rightmost position is the width minus one. I must be 
in the range to 255, TAB may only be used in PRINT. 

EXAMPLE: 



10 PRINT "NAME 


TAB(15) "AMOUNT' : PRINT 


20 READ AS, BS 




30 PRINT AS TAB(15)B$ 


40 DATA "G.T. JONES", "$25.00" 


RUN 




NAME 


AMOUNT 


G. T. JONES 


$25.00 


READY 




TAN 




Format: 


Abbreviation: Sci 



TAN(X) 



None 



Screen Display: 

None 



Action: Returnsthetangentof X in radians. TAN{X) is calculated in 
binary. If TAN overflows, the "OVERFLOW" error message is 
displayed. 

EXAMPLE: 

10 Y - Q*TAN{X)/2 



TIME 

Format : 

Tl 



Abbreviation : 

None 



Screen Olsplay: 

None 



51 



Action: Used to read the internal interval timer and return a 
value in one-tenth seconds. This is a real-time clock. This 
value is initialized only when Tl$ is envoked. 

EXAMPLE: 

10 PRINT TI.60 "SECONDS SINCE POWER UP- 
TIMES 

Format : Abbreviation : Screen Display: 

Tl$ None None 



Action : Used to read the internal inten/al timer and return a string of 
6 characters in hours, minutes, seconds. May be used in an input 
statement or on the left hand side of an expression to initialize the 
timer. 

EXAMPLE: 

10 Tl$ = "000000" 

20 FOR 1 = 1 TO 10000:NEXT 

30 PRINT TIS 

RUN 

000010 

READY. 



USR 

Format : Abbreviation : Screen Display: 



usR(x) uBsniTgas u[v 



Action; Calls the user's assembly language subroutine whose 
starling address is stored in locations 1 and 2. The argument is 
stored in the floating point accumulator (see memory map), and the 
result Is the value residing there when the routine returns to BASIC. 



L 

r 
r 



52 



EXAMPLE: 

40 B = T*SIN(Y) 
50 C = USR(B'2) 
60 D = USR(B/3) 
etc. 



VAL 

Format: Abbreviation : Screen Display; 



vAL(xs) wmssMA V 



Action: Returns the numerical value of string XS. If the first 
character of XS is not +, -. S, or a digit, VAL{XS) = 



EXAIVIPLE: 

10 READ NAMES, CITYS, STATES, ZIP$ 

20 IF VAL (ZIPS) < 90000 OR VAL (ZIP$) > 96699 THEN PRINT 

NAMES TAB{25) "OUT OF STATE" 

30 IF VAL{ZIPS)> =90801 AND VAL(ZIP$)< =90815 THEN 

PRINT NAMES TAB{25) "LONG BEACH" 

40 DATA "SUE M.", "MEDIA", "PA", "19063" 

(See the STRS function for numeric to string conversion.) 



S3 



NUMBERS AND VARIABLES 



The numbers printed by VIC 20 are governed by limitations within 
their formats. The numbers are stored internally to over ten digits of 
accuracy. However, when a number is printed, only nine digits are 
displayed. Each number may also have an exponent (a 
power-of-ten scaling factor). 

Numbers are used all the time when working with VIC 20. There 
are two kinds of numbers that can be stored : floating point numbers 
(also called real numbers) and integers. 

Floating point is the standard number representation used by the 
VIC, The VIC does its arithmetic using floating point numbers. A 
floating point number can be a whole number, a fractional number 
preceded by a decimal point, or a combination. The number can be 
signed negative ( - ) or positive ( -- ). If the number has no sign, it is 
assumed to be positive. 

Consider the following examples of floating point numbers: 

Whole number equivalent to an integer: 

5 

-15 

65000 

161 



Numbers with a decimal point: 
0.5 

0.0165432 
-0.00009 

1.6 
24,0085 
-65.6 
3.1416 

Notice that if you put a comma in a number and ask the VIC to 
assign it to a variable, you will get a Syntax Error message. For 
example, you must use 65000, not 65,000. 

Numbers always have at least eight digits of precision; they can 
have up to nine, depending on the number. The ViC rounds any 
additionai significant digits. It rounds up when the next digit is five or 
more; It rounds down when the next digit is four or less. 

Rounding numbers will sometimes cause fractional numbers to 
look inaccurate. Here are some examples: 

54 



r 



[ 
[ 
r 



r 
[ 



You type: ?.5555555556 

VIC prints: .555555555 

(VIC appears to round down on 6 or less; up on 7 or more,) 

You type: S.5555555557 

VIC prints: .555555556 

You type: 7.1111111115 

VIC types: .111111111 

(VIC appears to round down on 5 or less; up on 6 or more). 

You type: 7.1111111116 

VIC types: .111111112 

These quirks result from the manner in whioh computers store 
floating point numbers. 

Floating point numbers can also be represented in scientific 
notation. When numbers with ten or more digits are entered, the 
VIC automaticafly converts them to scientific notation. Scientific 
notation allows the VIC to accurately display these large numbers 
using fewer digits. For example: 

READY. 
71111111114 
1.11111111E + 09 

READY. 

71111111115 

1-11111112E-09 

A number in scientific notation has the form: 

numberEiiiee 

Where: 

number is an integer, fraction, or combination, as illustrated 
above. The "number" portion contains the number's 
significant digits; it is called the "coefficient." If no 
decimal point appears, it is assumed to be to the 
right of the coefficient. 

E is the upper case letter E. 

± is an optional plus sign or minus sign which indicates 

the sign of the exponent. 

BB is a one- or two-digit exponent. The exponent 

specifies the magnitude of the number; that is, the 
number of places to the right (positive exponent) or 
to the left (negative exponent) that the decimal point 
must be moved to give the true decimal point 
location. 

55 



Here are some examples: 



Scientific Notation 


Standard Notation 


2E1 


20 


10.5E + 4 


105000 


66E + 2 


6600 


66E-2 


0.66 


-66E-2 


-0.66 


1E-10 


0.0000000001 


94E20 


9400000000000000000000 



r 



As the I ast two exam pies show, scientific notation is a much more 
convenient way of expressing very large or very small numbers. 
VIC BASIC prints numbers ranging between 0.01 and 999,999,999 
using standard notation; however, numbers outside of this range 
are printed using scientific notation. 

Consider the following out-of-range examples: 

?.009 
9E-03 

READY. 
?.01 
.01 

READY. 
9999999998.9 
999999999 

READY. 
7999999999.6 

1E + 09 

There is a limit to the magnitude of a number that the VIC can [ 

handle, even in scientific notation. The range limits are: 



r 

L 
L 
L 



[ 



Largest floating point number: =1.70141183E-r38 
Smallest floating point number: ±2.93873588E-39 

Any number of larger magnitude will give an overflow error. For 
example: I 

?1. 701411 84E + 38 I 

70VERFL0W ERROR , 

READY. 

Any number of a smaller magnitude will yield a zero result. For 
example: p 

72.93873587E-39 



1 



L 



READY, 

An integer is a number that has no fractional portion or decimai 
point. The number can be signed negative ( - ) or positive { -f ), An 
unsigned number is assumed to be positive, integer numbers have 
a limited range of values, from -32768 to 4-32767. 

The following are examples of integers: 


1 

44 

32699 

-15 

Any number that is an integer can also be represented in floating 
point format since integers are a subset of floating point numbers. 
VIC BASIC converts any integers to floating point representation 
before doing arithmetic with them. The most important difference 
between floating point numbers and integers is that an integer array 
uses less storage space in memory (two bytes for an integer, 
versus five bytes for a floating point number). 

We have already used strings as messages to be printed on the 
display screen. A string consists of one or more characters 
enclosed in double quotation marks. 

Consider the following examples of strings: 

^■SYNERGY" 

"12345" 

■^$10.89 IS THE AMOUNT' 

All of the data keys (alphabetic, numeric, special symbols, and 
graphics), the three cursor control keys (Clear Screen/Home, 
Cursor Up/Down, Cursor Left/Right), as well as the Reverse On/Off 
key, insert/Delete, and Stop keys can be included in a string. The 
only keys that cannot be used within a string are Return, CTRL, 
Shift, and the Logo key. 

All characters within the string are displayed as they appear. The 
cursor control and Reverse On/Off keys, however, normally do not 
print anything themselves; to show that they are present in a string, 
certain reverse field symbols are used. They are shown in Tabfe 
2-1, 

When you enter a string from the keyboard, it can have any 
length up to the space available within an 88-Gharacter line (that is, 
any character spaces not taken up by the line number and other 
required parts of the statement). However, strings of up to 255 
characters can be stored in the VIC's memory. You get long strings 

57 



by pushing together, or concatenating, two separate strings to form 
one longer string. We will describe this further when we discuss 
string variables in general. 

Earlier in the chapter, we introduced the concept of a variable. In 
this discussion variables are described more thoroughly. 

A variable is a data item whose value may be changed. The value 
is determined by the number assigned to the variable. If you type 
the immediate mode statement: 

PRINT 10, 20. 30 

10 20 30 

The VIC will display the same three numbers (as illustrated 
above) whenever the PRINT statement is executed; that is 
because this PRINT statement used constant data. However, you 
can write the immediate mode statement: 

A=1D: B = 20: C = 30: PRINT A, B, C 
10 20 30 

The same three numbers, 1 0, 20, 30, are displayed; however, A, 
B, and C are variables, not constants. By changing the values 
assigned to A. B, and C, you can change the values printed out by 
the PRINT statement. Consider the following example of this: 

A= ^4: B = 45: C = 4E2: PRINT A, B, C 
-4 45 400 

You will notice that variables appear in virtually all computer 
programs. 

Variables are identified by names. We used A, B, and C as 
variable names in the illustrations above. A variable has two parts: 
its name and a value. The variable name represents a location at 
which the current value is stored. In the following illustration, the 
current value of A is 14; for B it is 15; and for C it is 0. 

Variable 

Name Contents 
A 14 

B 15 

C 

If we change A to - 1 using the immediate mode statement: 

A=-1 

then the Location Contents, stored under the variable-name A, will 
change from 14 to -1. 
This is an excellent way of looking at variables because it is, in 



58 



fact, the way they are handled by the VIC. A variable name 
represents an address In memory; and at that memory location, the 
current value of the variable is stored. The important point to note is 
that variable names— which are names thai programmers make 
up — are arbitrary; they have no innate relationship to the vafue that 
the variables represent. 

A variable name can have one or two characters. The first 
character must be an alphabetic character from A to Z; the second 
character can be either alphabetic or numeric {numeric characters 
must be in the range from to 9). A third character can be included 
to indicate the type of number (integer or string), % or S. 

Floating variables represent floating point numbers. This is 
probably the most common type of variable that you will use. 

The following are examples of floating point variables: 

A 

B 
A1 
AA 
Z5 

Integer variables represent integers. Names for integer variables 
are followed by the % symbol as the following examples indicate: 

A% 

B% 

A1% 

MN% 

X4% 

Remember, floating point variables can also represent integers; 
but you should use integer variables in arrays whenever possible 
since they use less memory— two bytes versus five for a floating 
point array element, 

A string variable represents a string of text. The following are 
examples of string variables: 

A$ 

M$ 

MN$ 

MIS 

2XS 

F6S 

You can use variable names having more than two alphanumeric 
characters; but if you do, only the first two characters count. To VIC 



59 



ARRAYS 

An array Is a sequence o1 related variables. A table of numbers, 
for example, may be visualized as an array. The individual numbers 
within the table become "elements'^ of the array. 

Arrays are a useful shorthand means of describing a large 
number of related variables. Consider, for example, a table of 
numbers containing ten rows of numbers, with twenty numbers in 

60 



I 
[ 



[ 



BASIC, therefore, BANANA and BANDAGE are the same name 
since both begin with BA. 

The advantage of using longer variable names is that they make 
programs easier to read. PARTNO, for example, is more 
meaningful than PA as a variable name describing a part number in 
an inventory program. 

VIC BASIC allows variable names to have up to 255 characters. 
The following are examples of names with more than the minimum 
number of characters: 

MEMBERS 
T1 234567 
BBSS 

ABCDFG% 
PARTY 

If you use extended variable names, keep in mind the following: 

1. Only the first two characters, plus the identifier symbol, are 
significant in identifying a variable. Do not use extended 
names like L00P1 and LOOP2; these refer to the same 
variable: LO. 

2. VIC BASIC has what are called "reserved words." These are 
words that have special meaning tor the VIC BASIC 
interpreter. No variable can contain a reserved word. In longer 
names you have to be very careful that a reserved word does 
not occur embedded anywhere in the name. 

3. The additional characters need extra memory space, which 
you might need for longer programs. 

The BASIC interpreter recognizes certain words as requests for 
specific operations. Names that are used to call up certain 
operations are called ^'reseii/ed words." These words cannot be I 

used as variable names because the interpreter will recognize the 
word as a request for the corresponding operation. Moreover, you I 

cannot use a reserved word as any part of your variable name; | 

BASIC will still find it and treat it as a request for an operation. 



L 



L 



each row. There are 200 numbers in the table. How woufd you like it 
if you have to assign a unique name to each of the 200 numbers? It 
would be far simpler to give the entire table one name, and identify 
individual numbers within the table by their table location. That is 
precise [y what an array does for you. 

Arrays of up to eleven elements (subscripts to 10 for a 
one-dimensional array) may be used where needed in VIC BASfC, 
just as variables can be used as needed. Arrays containing more 
than eleven elements need to be ^"declared" in a Dimension 
statement. Dimension statements are described later in this and in 
Chapter 3. An array (always with subschpts) and a single variable 
of the same name are treated as separate items by VIC BASIC. 
Once dimensioned, an array cannot be referenced with different 
dimensions. 



61 



OPERATORS 



An operator is a special symbol that VIC BASIC recognizes as 
representing an operation to be performed on the variables or 
constant data. One or more operators, combined with one or more 
terms, form an '^expression. '^ 

VIC BASIC provides arithmetic operators, relational operators, 
and Boolean operators. 

An arithmetic operator defines an arithmetic operation to be 
performed on the adjoining terms. Arithmetic operations are 
performed using floating point numbers, integers are converted to 
floating point numbers before an arithmetic operation is performed; 
the result is converted back to an integer. Consider the following 
operations and their symbols: 

a. Addition ( -^ ). The plus sign specifies that the term on the left 
is to be added to the term on the right. For numeric quantities this is 
straightfonA^ard addition. Examples: 

2 + 2 

A+B-rC 

X%+1 

BR + 10E-2 

The plus sign can be used to add strings; but rather than adding 
their values, they are joined together, or concatenated, forming one 
longer string. The difference between numeric addition and string 
concatenation can be visualized as follows: 

Addition numbers: 
num1 +num2==num3 

Addition of strings: 
string 1 +string2 = string1string2 

By concatenation, strings containing up to 255 characters can be 
developed. 

EXAMPLES: 

'TOR" + "WARD'^ results in "FORWARD" 
"Hl'^ + "THERE" results in ^'HITHERE" 

A$+B$ results in ASBS 

b. Subtraction ( - ). The minus sign specifies that the term to the 
right of the minus sign is to be subtracted from the term to the left, I 

62 r 



r 



r 



EXAMPLES: 

4 - 1 results in 3 

100-64 results m 36 

A~B results in the difference between 

the value of the two variables. 
S5-142 results in -87 

The minus can also be used as a unary minus; that is, the minus 
sign preceding a negative number. 



EXAMPLES: 

-S 

-9E4 

-B 

4 — 2 same as 4 + 2 

c. Multiplication (*). An asterisk specifies that the term on the 
right IS multiplied by the term on the left. 

EXAMPLES: 

100*2 results in 200 

50*0 results in 

A*X1 
R%M4 

d. Division (f). The slash specifies that the term on the left is to be 
divided by the term on the right. 

EXAMPLES: 

10/2 results in 5 

6400/4 results in 1600 

A"B 
4E2/XR 

e. Exponentiation ( f ). The up arrow specifies that the term on 
the left is raised to the power specified by the term on the right. If the 
term on the right is 2, the number on the left is squared; if the term 
on the right is 3, the number on the left is cubed, etc. The exponent 
can be any number, variable, or expression, as long as the 
exponentiation yields a number in the VICs range. 

63 



EXAMPLES: 




2|2 

12T2 

1T3 


results in 4 
results in 144 
results in 1 



When an expression has multiple operations, as in: 
A-hCM0/2t2 

There is a built-in hierarchy for evaluating the expression. First, 
the exponentiation is considered, followed by the unary minus ( - ), 
followed by the multfpllcation and division {*/). followed then by the 
addition and subtraction (+ -). Operators of the same hierarchy 
are evaluated from left to right. 

This natural order of operation can be overridden by the use of 
parentheses. Any operation within parentheses is performed first- 

EXAMPLES: 

4 + 1*2 results in 6 

(4 + 1)*2 results in 10 

100*4/2-1 results in 199 

100*(4/2-1) results in 100 

100*(4/(2-1)} results in 400 

When parentheses are present, VIC BASIC evaluates the 
innermost set first, then the next innermost, etc. Parentheses can 
be nested to any level and may be used freely to clarify the order of 
operations being performed in an expression. 

A relational operator specifies a "true" or "lalse^^ relationship 
between adjacent terms. The specified comparison is made, and 
then the relational expression is replaced by a value of true ( - 1 ) or 
false (0). Relational operators are evaluated after all arithmetic 
operations have been performed. 



EXAMPLES: 




1=5-4 


results in true (-1) 


14>66 


results in false (0) 


15> = 15 


results in true (-1) 



Relational operators can be used to compare strings. For 
comparison purposes, the letters of the alphabet have the order 
A<B, B<C, C<D, etc. Strings are compared by comparing their 
stored character values. Characters are stored using a special 



L 



[ 



64 



binary code called "ASCII." The Appendix lists the ASCII code 
assigned to every VIC character. 

EXAMPLES: 

''A"<"B" results in true (-1) 

"X" = "XX" results in false (0) 

C$ = A$-rBS 

The Boolean operators AND, OR, and NOT specify a Boolean 
logic operation to be performed on two varlabies, on adjacent sides 
of the operator. In the case of NOT, only the term to the right is 
considered. Boolean operations are not performed until all 
ahthmetic and relational operations have been compJeted. 

EXAMPLES: 

IF A= 100 AND 8=100 THEN 10 

If both A and B are equai to 100, branch to 

statement 10. 
IF X<Y AND B>-44 THEN F = 

If X Is less than Y, and B is greater than 

or equal to 44, then set F equal to 0. 

IF A=100 or B = 100 THEN 20 
If either A or B has a value of 100, branch to 
statement 20. 

IF X<Y OR B> = 44 THEN F = 
F is set to if X is less than Y, or B is greater 
than 43. 

A single term being tested for "true" or "false" can be specified by 
the term itself, with an implied "<>0" following it. Any non-zero 
value is considered true; a zero value is considered false. 

EXAMPLES: 

fF A THEN B = 2 

IF AOOTHEN B = 2 

The above two statements are equivalent. 
IF NOT B THEN 100 

Branch if B is false, i.e., equal to zero. This is 
probably better written as: 
IF B-OTHEN 100 



65 



The three Boolean operators can also be used to perform logic 
operations on the individual binary digits of two adjacent terms (or 
just the term to the right in the case of NOT), But the terms must be 
in the integer range. Boolean operations are defined by groups of 
statements, which taken together constitute a "truth table/' The 
following table lists the truth tables for the Boolean operators used 
by VIC BASIC. 



Boolean Truth Table 



The AND operation results in a 1 only if both bits are 1. 
1 AND 1 = 1 

AND 1 = 

1 AND = 
AND = 

The OR operation results in a 1 if either bit is 1, 
1 OR 1 = 1 

OR 1 = 1 

1 OR = 1 
OR = 

The NOT operation logically comptements each bit. 

NOT 1 - 
NOT = 1 



r 



This discussion of binary digit (bit) oriented Boolean operations is 
presented for those who are interested in the details of how these 
operations are performed. If you do not understand it, skip it. You 
are not skipping anything you must know. Recall that a single term 
has an implied "<>0" following it. The expression therefore 
becomes: 

IF OOO GOTO 40 

Thus, the branch is not taken. 

In contrast, a Boolean operation performed on two variables may 
yield any integer number: 

!F A% AND 8% GOTO 40 

Assume that A% - 255 and B% - 240. The Boolean operation 265 
AND 240 yields 240. The statement, therefore, is equivalent to: 

66 



L 

r 



r 

L 



IF 240 GOTO 40 

or, with the "<>0": 

IF 240 <>0 GOTO 40 

Therefore the branch will be taken. 
Now compare the two assignment statements' 

A = A AND 10 
A = A<10 

fn the first example, the current value of A is logically ANDed with 
10 and the result becomes the new value of A. A must be in the 
integer range -^32767 to -32768. In the second example, the 
relational expression A<1 is evaluated to - 1 or 0, so A must end 
up with a value of -1 or 0- 



m 



LOGICAL OPERATORS 

Logical operators perform tests on multiple relations, bit 
manipulation, or Boolean operations. The logical operator returns a 
bitwise result which is either "true" (not zero) or 'false" (zero). In an 
expression, logical operations are performed after arithmetic and 
relational operations. The outcome of a logical operation is 
determined as shown in the following table. The operators are listed 
in order of precedence, 

NOT 

X NOT X 



1 
1 






AND 






X 


Y 


X ANDY 


1 
1 





1 

1 



1 






r 



OR 



X 


Y 


X OR Y 


1 


1 


1 


1 





1 





1 


1 












r 



Just as the relational operators can be used to make decisions 
regarding program flow, logical operators can connect two or more 
relations and return a true or false value to be used in a decision. 
For example: 

IF D<200 AND F<4 THEN 80 
IF [>10 OR K<OTHEN 50 
IF NOT PTHEN 100 



68 



r 

L 



Logical operators work by converting their operands to 
sixteen-bit, signed, two's-complement integers in the range 
- 32768 to ^ 32767. (If the operands are not in this range, an error 
results.) If both operands are supplied as or - 1 , logical operators 
return or - 1 . The given operation is performed on these integers 
in bitwise fashion, i.e., each bit of the result is determined by the 
corresponding bits in the two operands. 

Thus, it IS possible to use logical operators to test bytes for a 
particular bit pattern. For example, the AND operator may be used 
to ^"mask" all but one of the bits of a status byte at a machine I/O 
port. The OR operator may be used to "merge" two bytes to create 
a particular binary value. The foflowing examples will demonstrate 
how the logical operators work: 

63 AND 16 = 16 63 = binary 111111 and 16 = binary 
10000, so 63 AND 16=16 

15 AND 14 = 14 15 = binary 1111 and 14 = binary 

1110, so 15 AND 14 - 14 (binary 1110) 

~1 AND 8 = 8 -1 = binary 11111111 and 8 = 
binary 1000, so -1 AND 8 = 8 

4 OR 2 = 6 4 = binary 100 and 2 = binary 10, so 4 
or 2 =- 6 (binary 110) 

10 OR 10 = 10 10 = binary 1010, so 1010 OR 1010 = 
1010 (10) 

-1 OR -2 = -1 -1 = binary 11111111 and -2 = binary 
11111110, so 

- 1 OR - 2 = - 1 . The bit complement of 
sixteen zeros is sixteen ones, which 
is the two's complement representation 
of - 1 . 

NOT X= (-X}-r 1 The two's complement of any integer is 
the bit complement plus one. 

STRING OPERATIONS 

Strings may be concatenated using +. For example: 

10 A$ = 'TILE" : B$-^^NAME'^ 

20 PRINT AS - BS 

30 PRINT "NEW" + AS 4- BS 

RUN 

FILENAME 

NEW FILENAME 



69 



Strings may be compared using the same relational operators that 
are used with numbers: 

= <> < > <= > = 

String comparisons are made by tal<ing one character at a time 
from each string and comparing the ASCII codes. If all the ASCII 
codes are the same, the strings are equal. If the ASCII codes differ, 
the lower code number precedes the higher. If, during string 
comparison, the end of one string is reached, the shorter string is 
said to be smaller. Leading and trailing blanks are significant. 
Examples: 

"AA" < "AB" 

-FILENAME" = "FILENAME'^ 

"X$" > "X#" 

"CL " > XL'* 

"kg" < "KG" 

"SMYTH" < "SMYTHE" 

BS < "9/12/78" where B$ = "8/12/78" 



r 



r 



70 



L 
[ 
[ 
[ 

r 
[ 
[ 
[ 
[ 
[ 
[ 
[ 
[ 
[ 
[ 



EDITING PROGRAMS 

CURSOR CONTROLS 

One of the most important features of the VIC is the ability to 
move the cursor around the screen and make changes to program 
lines. This is called "screen editing/' The VIC will allow cursor 
movement around the screen in any direction, inserting extra 
spaces into a line, and erasing unwanted characters. The VIC'S 
editor is one of the most powerf ui and easy to use of any computer. 

Ther e are 6 keys on the keyboard that are used for editing. These 

''^'^^B' H- @' M' ESD' '""^ t^'ESS 

bar These are afl d ual purpo se keys, with different actions 
when the ^^^^ or ^^i3 keys are pressed. The ^M , 
Mil ' ^^^ R3!fifS ^'^' ^" ^^P^^^ 'f ^6^cl down for at 



least a second. 



When unshifted, this moves the cursor to the upper left corner of 
the screen, called the "home posi tion/' 

With the ^^^^1 (or [3 key) held down, this key will 

erase everything on the screen and move the cursor to the home 

position. This does not erase a program, variables, or anything else 

currently in memory. 



b. 

Without the ^ggf , this key serves to delete the character to 
the left of the cursor. Anything else on the line to the right of the de- 
leted characteMsmoved one space to the left, filling in the gap. 

With the ^^^^1 , this becomes an insert key. A space is cre- 
ated atthe cursors position, and everything else on the line to the 
right of the cursor is moved one more space to the right, if this 
pushes the last character on the line past the right end of the line, all 
lines below the current one are pushed down one line. Once the 
line is filled all the way to the end of 4 screen lines (88 characters), 
this key has no effect. (See also QUOTE MODE.) 

73 



c 

This will move the cursor up or down one line on the screen, 
without affecting anything displayed on the scre en. Unshif ted, the 
curs or moves down, and if you hold down the ^^^^1 (or the i 

[#3 key) the cursor moves up, L 



d. 

This causes the cursor to move one space sideways. Unshifted, 
the move is to the right; and shifted, the cursor moves left. All 
characters remain on the screen without being erased. Notice that if 
the cursor moves beyond the right edge of the screen, it "wraps" 
one line down, to the left edge, if you move to the left edge the 
cursor wraps to the right side of the next line up (except from the 
home position). 



RETURN 



The primary purpose of this key is to "enter" an instruction, 
calculation, or line of instructions. In direct mode the ViC executes 
the instruction or caiculation. In program mode (when the 
instruction is preceded by a line number) the RETURN key causes 
the program line to be stored in memory. However, when you hold 
down the SH IFT key and hit RETURN, the ViC moves the cursor to 
the next line and the left edge of the screen but does not affect the 
line or instruction— SHI FT RETURN is a fast method for moving the 
cursor down the screen. 



SPACE 



f. 

When you hit the ^^^H bar {at the bottom of the keyboard) a 
blank space appears on the screen and the cursor moves one 
space to the right, erasing any character that was previously on that 
position. ^^^^^ 

If the UliJim or [^ keys are held down while typing the 
^^^3 bar, a character is printed that looks like a space but is 
actually treated by the VIC as a graphic character. 



EDITING LINES 

Anything displayed on the screen can be edited using the cursor 
controls. This can be a program line that is LISTed or typed in, or a 
command without a line number. To edit a line simply move the 

74 



f 



cursor until it is on the line, then make the required changes, 
inserting or deleting as needed. Once you are finished, just hit the 
^^^^ key. All the changes will be stored as made. If yo u just 
want to getthecursor past the line it ts on, just hold ^^^Hwhile 
hitting ^^^^ and the VIC will ignore the lines it passes. 

in or der to delet e a line from a program, just type the line number 
and hit ^^^^ . There is no command for deleting more than 1 
fine at a time, although there is a trick for erasing all but a few lines. 
This involves LISTing the good portion of the program on the 
screen, typ ing NEW, moving the cursor to the top line displayed, 
and hitting ^^^ffl' on all the lines. This method only works if the 
section to be kept is very small (less than one screenful). 

a. Direct Mode/^Calculator" Mode 

If you enter a command or set of commands without a line 

number, they will execute as soon as you press the ^^fflffl key. 

This is called "Direct Mode" or "Immediate fvlode." 

Since the VIC allows multiple statements on a line by using the 
colon (:), you can actually get a short program to run without 
entering line numbers. This is especially helpful when there is 
already a program in memory that you don't want to disturb. The 
maximum length of a program line is 4 screen lines, or 88 
characters. In other words, if you enter a line numbered 10 you can 
display four 22-character lines on the screen but the VIC wilt store 
and interpret the information as one 88-character program line. 
Here is a sample im mediate mode program lor a sound effect: 

(don't hit ^^^^n until you've typed the whole line) 

POKE 36878,15:FOR L-254 To 128 STEP-1:P0KE 36876 
L:POKE 36876,0:NEXT:POKE 36878,0 

Some people also call this 'calculator mode," because one of the 
most common things you can do with a VIC is perform calculations 
that don't require a program. 

Here are some examples of calculations: 
PRINT 5-r4 
PRINT (2 + 4)*5/9 
PRINT S1N{37) 

Certain commands can't be used in direct mode. These are: 
INPUT, INPUT#, GET, GET#, and DEF FN. The DATA statement 

75 



may be typed but has absolutely no effect. The first four commands 
shown can't be used because the same buffer that holds the 
statement being executed is also used to hold the characters being 
input. The DEF FN statement requires a line number so that the 
formula may be referenced later. 

b. Program Mode 

Instructions beyond a basic level of complexity require a 
program, as opposed to direct mode commands which can perform 
simple commands in a single line of instructions. A program Is one 
or more tirjes, each having its own unique number and containing 
a statement or group of statements. Line numbers must be whoie 
numbers from to 63999. Programs are usually written using every 
10th number, or even 100th number, since most programmers 
want to add more lines later between two existing lines, as the 
program is developed or edited. 

Line numbers are stored in numerical order regardless of the 
order in which they are typed. The program will, when RUN, 
execute from the lowest to highest numbered lines, unless there is 
a command to jump to a different line, like GOTO, IF . . . THEN, or 
GOSUB, 



76 



USING THE GET STATEMENT 

Most simple programs use the INPUT statement to get data from 
the person operating the computer. When dealing with more 
complex needs, such as protection from typing errors, the GET 
statement offers more flexibility and gives the program more 
intelligence. This section shows you how to use the GET statement 
to add some special screen editing features to your programs. 

The VfC has a keyboard buffer that holds up to 10 characters. 
This means if the computer is busy doing some operation and is not 
reading the keyboard, you can still type in up to 1 characters, and 
the VIC will use them as soon as it finishes what it was doing. 

This can be demonstrated with a simple program. Type in the 
program shown below. When you tell it to RUN, type the word 
HELLO on the keyboard. Since the VIC is busy in a loop, nothing 
appears on the screen— until the program stops after 15 seconds, 
and the word HELLO that you typed appears on the screen. 

10 TIS = "000000" 

20 [FTIS < ^TOOOIS" THEN 20 

The VIC^s input buffer is also called a queue, which is a good 
image to use to better understand how it works. Imagine standing In 
line watting to buy a ticket to get into a movie. The first person in line 
is the first to get a ticket and leave the line, and the last person in line 
Is the last to get a ticket. (In accounting, this is called the "first in, first 
out" method, or FIFO, as opposed to the "last in, first out", or LIFO 
method.) 

The GET statement in the VIC acts as the ticket taker. First it 
looks to see if there are any characters "in line" (if a key or keys 
have been typed). If there are, the first character typed gets placed 
in a "variable" and out of the queue. If no characters are waiting in 
the buffer, then an empty value is returned. 

One other point should be mentioned when talking about the 
queue. Any characters typed on the VfC's keyboard afterthe queue 
is full are lost, since the queue was full. So imagine that the ticket 
line is long enough to hofd 1 people, and there is a cliff at the end of 
the line. Anyone trying to get into the line after the line is full simply 
falls off the cliff, never to be seen again. 

Since the GET statement will keep going even when no character 
was typed, it is often necessary to put the GET statement Into a 
loop, having it wait until the operator hits a key (actually, until a 
character has been received). Here is the recommended form: 

(Type NEW to erase the previous program.) 
10 GET AS : IF AS = "" THEN 10 

77 



There must be NO SPACE between the quotes in this line, to 
indicate an empty value. When the person is not typing anything, 
the empty value goes into the string variable (in this case 
represented by AS and the IF statement sends the program back to 
the GET statement. This loop repeats indefinitely, until the person 
operating the computer hits any key on the keyboard. At this point, 
the program continues with the line following line 10. 

Add this line to the program: 

100 PRINT AS; : GOTO 10 

Now RU N the program . No cursor appears on the screen, but any 
character you type will be printed on the screen. This includes all 
special functions, like cursor and color controls and clearing the 
screen. This two-line program can be developed into a screen 
editor, shown below. 

There are many features that you could use in a screen editor. A 
flashing cursor is nice to have, and can be prog rammed. Also, you 
may wish to "trap" certain keys, like ||j||| , so as not to 
erase the screen accidentally. Or you may wish to program the 
function keys for whole words. The following lines give each 
function key a special purpose. Remember, this is only the 
beginning of a program that you can customize for your needs, like 
word processing or data capture. 

CHRS(133) THEN POKE 368793: GOTO 10 
CHR$(134) THEN POKE 36879,27: GOTO 10 
CHR$(135) THEN A$='"DEAR SIR:" + 
CHRS(13} 
50 IF AS - CHR$(136) THEN A$ = '^SINCERELY,'' -f 
CHRS(13) 

The CHRS numbers in the parentheses come from the CHRS 
code chart in the Appendix, which lists a different number for each 
key character. The four function keys are activated here to perform 
the tasks represented by the instructions which immediately follow 
the word THEN in each line ... of course you could designate 
different keys by changing the CHRS number in parentheses, and 
different instructions after the THEN statement. 



20 IF AS 


30 IF AS 


40 IF AS 



L 



78 



HOW TO CRUNCH BASIC 
PROGRAMS 

You can pack more instructfons— and power— Into your BASIC 
programs by making each program as short as possible. This 
process of shortening programs is called "crunching." 

Crunching programs lets you squeeze the maximum possible 
number ot instructions into your program. It also helps you reduce 
the size of programs which might not otherwise run in a given size; 
and if you Ve writing a program which requires the input of data such 
as inventory items, numbers or text, a short program will leave more 
memory space free to hold data. 

But whether youYe using an unexpanded VIC or a 32K VIC 
System, your programs will benefit from the following crunching 
techniques. 

ABBREVIATING KEYWORDS 

A list of keyword abbreviations is given in the Appendix A. This is 
helpful when you program because you can actually crowd more 
information on each line using abbreviations. The most frequently 
used abbreviation Is the question mark (?) which is the BASIC 
abbreviation for the PRINT command. However, if you LIST a 
program that has abbreviations, the VIC will automatically print out 
the listing with the full-length keywords. If any program line exceeds 
88 characters (4 lines on the screen) with the keywords 
unabbreviated, and you want to change it, you will have to re-enter 
that line with the abbreviations before saving the program. 
SAVEing a program incorporates the keywords without inflating 
any lines because BASIC keywords are token ized by the VIC. 
Usually, abbreviations are added after a program is written and do 
not have to be LISTed any more before SAVEing. 

SHORTENING PROGRAM LINE NUMBERS 

Most programmers start their programs at line 100 and number 
each line at intervals of 10 (i.e., 100, 120, 130). This allows extra 
lines of instruction to be added (111,112, etc.) as the program is 
developed. One means of crunching the program after it is 
completed is to change the line numbers to the lowest numbers 
possible (i.e., 1, 2, 3) because ionger line numbers take more 
memory than shorter numbers. For instance, the number 1 00 uses 

79 



3 bytes of memory {one for each number) while the number 1 uses 
only 1 byte. 

PUTTING MULTIPLE INSTRUCTIONS ON 
EACH LINE 

You can put more than one instruction on each numbered line in 
your program by separating them by a colon. The only limitation is 
that all the instructions on each line, including colons, should not 
exceed the standard 88-character line length. Here is an example 
of two programs, before and after crunching: 

BEFORE CRUNCHING: AFTER CRUNCHING: 

10 PRINT '^HELLO. . /'; 10 PRINT "HELLO. - .";:FOR 

T = 1 TO 500;NEXT: PRINT 
20 FORT=1TO500:NEXT "HELLO.AGAIN. . /'iGOTOlO 

30 PRINT "HELLO, AGAIN, . r 
40 GOT0 10 

REMOVING REM STATEMENTS 

REM statements are helpful in reminding yoursei^— or showing 
other programmers^what a particular section of a program is 
doing. However, when the program is completed and ready to use, 
you probably won't need those REM statements anymore and you 
can save quite a bit of space by removing the REM statements. If 
you plan to revise or study the program structure in the future, its a 
good idea to keep a copy on file with the REM statements intact. 

USING VARIABLES 

If a number, word or sentence is used repeatedly in your program 
it's usually best to define those long words or numbers with a one or 
two letter variable. Numbers can be defined as single letters. Words 
and sentences can be defined as string variables using a letter and 
dollar sign. Here^s one example: 

BEFORE CRUNCHING AFTER CRUNCHING 

10 POKE 36878, 15 10 POKE 36878. 15: S = 36874 

20 POKE 36S74, 200 30 POKES, 200:POKES, 250:POKES, 
30 POKE 36874. 250 150 

40 POKE 36874, 150 

SO 



\ 



c 



r 
[ 



USING READ AND DATA STATEMENTS 

Large amounts of data can be typed in as one piece of data at a 
time, over and over again . , . or you can print the instructional part 
of the program ONCE and print all the data to be handled in a long 
running list called the DATA statement. This is especially good for 
crowding large lists of numbers into a program, 

USING ARRAYS AND MATRICES 

Arrays and matrices are similar to DATA statements in that long 
amounts of data can be handled as a fist, with the data handling 
portion of the program drawing from that list, in sequence. Arrays 
differ in that the list can be two or three dimensional. 



ELIMINATING SPACES 

One of the easiest ways to reduce the size of your program is to 
eliminate all the spaces. Although we often include spaces in 
sample programs to provide clarity, you actuafly don't need any 
spaces in your program and will save space if you eliminate them. 

USING GOSUB ROUTINES 

If you use a particular line or instruction over and over, it might be 
wise to GOSUB to the line from several places in your program, 
rather than write the whole line or instruction every time you use it. 

USING TAB AND SPC 

Instead of PRINTing several cursor commands to position a 
character on the screen, it is often more economical to use the TAB 
and SPC instructions to position words or characters on the screen. 



81 



WORKING WITH GRAPHICS 

The graphics ability of the VIC 20 is more powertu! and 
sophisticated than many users realize. The following material is a 
concept-by-concept guide to help you make better use of these 
graphics features to enhance your games and other programs. 

CHARACTER MEMORY 

Each character is formed in an 8-by-8 grid of dots, where each 
dot may be either "on" or "off," The character images are stored in 
a special chip called the "Character Generator ROM." The 
characters are stored as a set of 8 bytes for each character, with 
each byte representing the dot pattern of a row in the character, and 
each bit representing a dot. A zero (0) bit means that dot is off, and 
a one (1) bit means the dot is on. 

The character memory in ROM begins at location 32768. The 
first 8 bytes contain the pattern for the (a sign, which has a 
character code value of zero on the screen. The next 8 byles, from 
location 32776 to 32783, contain the information for forming the 
letter A. 

IMAGE 



BINARY 


PEEK 


00011000 


24 


00100100 


36 


01000010 


66 


01111110 


126 


01000010 


66 


01000010 


66 


01000010 


66 


00000000 






Each complete character set takes up 2K of memory, 8 bytes per 
character and 256 characters. Since there are two character sets, 
one for upper case and graphics and the other with upper and lower 
case, the character generator ROM takes up a total of 4K. 

PROGRAMMABLE CHARACTERS 

Since the characters are stored in ROM, it would seem like there 
is no way to change them for customizing characters. However, the 
memory location that tells the VIC where to find the characters is in 
a RAM location in the VIC chip, which can be changed to point to 

32 



L 



[ 



many sections of mennory. By changing the character memory 
pointer to point to RAM, the character set may be programmed for 
any need. 
The VIC^s standard characters are stared as follows: 

HEX DECIMAL DESCRIPTION 

8000 32768 Upper case with full graphics 

8400 33792 Upper case & graphics— reversed 

8800 34816 Upper and lower case with some 

graphics 
8C0O 35840 Upper & lower with some graphics— re- 

versed 

The register which controls where the chip gets its character 
information fs at location 36869 decimal (9005 HEX). Its value is 
normally 240 {upper case and graphics) or 242 (upper/lower case). 

The programmed character set cannot be put into expansion 
RAM, since the VIC chip doesn^t have access to that memory. 
Therefore, any programmed characters must begin at a memory 
location between 4096 and 7168. Since BASIC programs are 
normally stored beginning at 4096. and strings start at the top of 
available memory and work their way down, precautions must be 
taken to protect the character set from being overwritten by BASIC, 
If the BASIC program begins at 4096, the norma! procedure is to 
change the pointers to the top of available RAM at locations 52 and 
56 so that they point below the character set. The folfowing chart 
shows the possible locations of character sets, and the POKES to 
protect them. 



Num- 


Location of 


Contents 




ber 


Characters 


of Location 


POKE 52 & 56 


240 


32768 


Character ROM 




241 


33792 


Character ROM 




242 


34816 


Character ROM 




243 


35840 


Character ROM 




244 


(36864) 


ViC Chip, 10 




245 


(37888) 


Color RAM 




246 


(38912) 


nothing i 




247 


(39936) 


nothing 




248 


(0) 


Zero Page RAM 




249 


(1024) 


Expansion RAM 




250 


(2048) 


Expansion RAM 




251 


(3192) 


Expansion RAM 




252 


4096 


Start of BASIC RAM 





83 



253 


5120 


BASIC RAM 


20 


254 


6144 


BASIC RAM 


24 


255 


7168 


BASIC RAM 


28 



L 



This table assumes that screen memory starts at 7680 (1 EOO). 
However, it can be moved to other locations. The number of 
characters you have to work with at each location might change in 
that case. 

There are two problems involved in creating your own special 
characters. First, it is an all or nothing process. Generally, if you use 
your own character set by telling the VIC chip to get the character 
information from the area you have prepared in RAM, the standard 
ViC characters are unavailable to you. To solve this problem, you 
must copy any letters, numbers, or standard VIC graphics you 
intend to use into your own character memory in RAM. You can pick 
and choose, take only the ones you want, and don't even have to 
keep them in order! 

The second problem with programmable characters is that your 
character set takes memory space away from your BASIC 
program. This is a trade off situation, since you only have a limited 
amount of RAM available. If you decide to create a character set for 
a program, the program has to be smaller than a program which 
uses the standard VIC characters. 

There are two locations in the VIC to start your character set that 
should not be used with BASIC — and 4096, The first should not 
be used because BASIC stores important data on page 0. The 
second can t be used because that is where your BASIC program 
starts! (If you expand your VIC. or use machine language, you can 
start your characters at 4096 if you want. This limit only applies to 
the unexpended VIC.) 

The best place to put your character set for use with BASIC while 
experimenting is at 71 68. This is done by POKEing location 36869 
with 255, giving you 64 characters to work with. Try the POKE now, 
like this: 

POKE 36869,255 

Immediately all the letters on the screen turn to garbage. This is 
because there are no characters set up at location 7168 right now 
. . , only random bytes. Set the VIC back to normal by using the 
RUN/STOP and RESTORE keys. 

Now let's begin creating graphics characters. To protect your 
character set from BASIC, you should reduce the amount of 
memory BASIC thinks that it has. The amount of memory in your 
computer stays the same . . . it's just that youVe told BASIC not to 
use some of it. 

84 



Type: 

PRINT FRE{0) 

The number displayed is the amount of memory space left 
unused. Now type the following: 

POKE 52. 28: POKE56, 28; CLR 
Now type: 

PRiNT FRE(O) 

See the change? BASIC now thinks it has 51 2 bytes fess memory 
to work with. The memory you just reclaimed from BASIC is where 
you are going to put your character set, safe from actions of BASIC. 

The next step is to put your characters into RAM. When you 
begin, there is random data beginning at 7168- You must put 
character patterns in RAI\^ (in the same style as the ones in ROM) 
for the VIC to use. 

The following one line program moves 64 characters from ROM 
to your character set RAM: 

FOR 1= 7166 TO 7679: POKE I. PEEK(I + 25600): NEXT 

Now POKE 36869 with 255. Nothing happens, right? Well, 
almost nothing. The VIC is now getting its character information 
from your RAM, instead of from ROM. But since we copied the 
characters from ROM exactfy, no difference can be seen . . . yet. 

You can easily change the characters now. Clear the screen and 
type an @ sign. Move the cursor down a couple of lines, then type: 

FOR I = 7168 TO 7168 + 7:POKE I, 255 - PEEK(I) : NEXT 
You just created a reversed @ sign! 




JT^ 



VIC TIP; Reversed characters are just characters with their bit 
patterns in character memory reversedl 



Now move the cursor up to the program again and hit return 
again to re-reverse the character {bring it back to normal). By 
looking at the table of screen display codes, you can figure out 
where in RAM each character is. Just remember that each 
character takes eight memory locations to store. 

Here are a few examples just to get you started: 



CHARAC- 


DISPLAY 


CURRENT STARTING LOCATION 


TER 


CODE 


IN RAM 


@ 





7168 


A 


1 


7176 


! 


33 


7432 


> 


62 


7664 



85 



Remember that we only took the first 64 characters, though. 
Something else will have to be done i1 you want one of the other 
characters. 

What if you wanted character number 154, a reversed Z? Weil, 
you could make it yourself, by reversing a Z, or you could copy the 
set of reversed characters from the ROM, or just take the one 
character you want from ROM and replace one of the characters 
you have in RAM that you don't need. 

Suppose you decide that you won't need the > sign. Let's 
replace the > sign with the reversed Z. Type this: 

FORI = 7664 TO 7671: POKE I, PEEK{I^ 26336): NEXT 

Now type a > sign. It comes up as a reversed Z. No matter how 
many times you type the >, it comes out as a reversed 2. (This 
change is really an illusion. Though the > sign looks like a reversed 
Z, it still acts like a > in a program. Try something that needs a > 
sign- It will still work fine, only it will fook strange.) 

A quick review: We can now copy characters from ROM into 
RAM . We can even pick and choose only the ones we want. There's 
only one step left in programmable characters {the best step!) . . . 
making your own characters. 

Remember how characters are stored in ROM? Each character 
is stored as a group of eight bytes. The bit patterns of the bytes 
directly control the character. If you arrange 8 bytes, one on top of 
another, and write out each byte as eight binary digits, it forms an 
eight-by-eight matrix, looking like the characters. When a bit is a 
one, there is a dot at that location. When a bit is a zero, there is a 
space at that location. 

When creating your own characters, you set up the same kind of 
table in memory. Type this program: 

10 FORC- 7328 TO 7335: READ A: POKE C,A: NEXT 

20 DATA 60, 66, 165, 129, 165, 153, 66, 60 

Now type RUN. The program will replace the ietter T with a smile 
face character. Type a few Ts to see the face. Each of the numbers 
in the DATA statement in line 20 is a row in the smile face character. 
The matrix for the face looks like this: 



7 6 5 4 3 2 10 



ROWO 
1 
2 
3 
4 
5 
6 

ROW 7 



L 



L 

r 



86 



DECIMAL 


BINARY 


60 


00111100 


66 


01000010 


165 


10100101 


129 


10000001 


165 


10100101 


153 


10011001 


66 


01000010 


60 


00111100 



The sheet on this page wilf help you design your own characters. 
There is an e-by-8 matrix on the sheet, with row numbers, and 
numbers at the top of each column. (If you view each row as a 
binary word, the numbers are the value of that bit position. Each is a 
power of 2. The leftmost bit is equal to 1 28 or 2 to the 7th powe r. the 
next is equal to 64 or 2 to the 6th, and so on, until you reach the 
rightmost bit (bit 0) which is equal to 1 or 2 to the 0th power) 

Place an X on the matrix at every location where you want a dot to 
be in your character. When your character is ready you can create 
the DATA statement for your character 

Begin with the first row. Wherever you placed an X, take the 
number at the top of the column, and write it down. When you have 
the numbers for every column of the first row, add them together 
Write this number down , next to the row. This is the nu m ber that you 
will put into the DATA statement to draw this row: 

Do the same thing with all of the other rows {1 -7). When you are 
finished you should have 8 numbers between and 255. If any of 
your numbers are not within range, recheck your addition. The 
numbers must be in this range to be correct! If you have less than 8 
numbers, you missed a row. It's OK if some are 0. The rows are 
just as important as the other numbers. 





7 


6 


5 


4 


3 


2 


1 
























1 


















2 


















3 


















4 


















5 


















6 


















7 



















Programmable Character Worksheet 



Replace the numbers in the DATA statement in line 20 with the 
numbers you just calculated, and RUN the program . Then type a T. 
Every time you type it, you see your own character! 

If you don't like the way the character turned out, just change the 
numbers in the DATA statement and re-RUN the program until you 
are happy with your character. 

That s all there is to it! 

HIGH RESOLUTION GRAPHICS 

When writing games or other types of programs, sooner or later 
you get to the point at which you want a high resolution display, or 
smooth movement of objects on the screen. A regular character 
can move one space at a time, which is 8 rows or columns of dots. 
For smoother movement, characters should be moved one row of 
dots at a time, using high-resolution graphics. 

The VIC can handle this need: high resolution is available 
through bit mapping the screen. Bit mapping is the name of the 
method where each possible dot (pixel) ot resolution on the screen 
is assigned its own bit in memory. If that memory bit is a one, the dot 
it is assigned to is on. If the bit is set to zero, the dot is off. You can bit 
map the entire screen of the VIC, or only a portion of it. You can mix 
HI-RES, programmable characters and regular graphics. 

High resolution has a tew drawbacks, which is why it is not used 
all the time. If takes a lot of memory to bit map the entire screen. 
Because every pixel must have a memory bit to control it, you are 
going to need one bit of memory per pixel (or one byte for 8 pixels). 
Since each character is 8-by-8, and there are 23 lines of 22 
characters, the resolution is 1 76 by 1 84 for the whole screen. That 
gives you 32384 separate dots, each of which requires a bit in 
memory, or 4048 bytes of memory needed to map the whole 
screen. 

Fear not, you can still use high resolution graphics on the 
unexpanded VIC! You just don't bit map the entire screen. Instead, 
you bit map just as much of the screen as you have memory for, and 
either use the rest of the screen as a border, or use it for text. A 64 
dot by 64 dot screen section will be fairly easy to work with for this 
section. 

Generally, high resolution operations are made of many short, 
simple, repetitive routines. Unfortunately, this kind of thing is rather 
slow using BASIC, so high resolution routines written in BASIC are 
usually rather slow. However, short, simple, repetitive routines are 
exactly what machine language does best. The solution is to either 
write your programs entirely in machine language (painful), or call 



L 



\ 



HI-RES subroutines from your BASIC program, using the SYS 
command from BASIC. That way you get both the ease of writing in 
BASIC, and the speed (for graphics) of machine language. The 
SUPER-EXPANDER cartridge also is available to add HI-RES 
commands to VIC BASIC. 

All of the examples given in thrs section wilt be in BASIC to make 
them clear. In the future, you can add the routines to your own 
programs to give you easy Hf-RES graphics. Now to the technical 
details. 

Remember programmable characters? Well, bit mapping is 
done almost the same way. When you created a programmable 
character, you could watch it form before your eyes, if the character 
was on the screen when you were changing it. 

To see this again, type this program In and RUN it, 

100 POKE 36869 , 255 

1 1 FOR I - 71 68 TO 7679 : POKE I, PEEK(I ^ 25600) : NEXT 

120 PRINT CHR$(147) 'A' 

130 FORI = 71 76 TO 7183 

140 FOR L - 1 TO 1000 : NEXT 

150 READ A : POKE I, A : NEXT 

160 DATA 60,36,36,36,36,36,255,255 

The character changed from an A to a top hat as you watched! 
This is the trick behind HI-RES graphics on the VIC — making 
changes directly on the character memory. When the character is 
already on the screen you see the changes right away! 

The best way to set up the HI-RES display screen for the 64-dot 
by 64-dot HI-RES display is to print out 64 characters in a square 
box or matrix. 

Setting up the HI-RES display screen is the first step in HI-RES 
graphics. The following short program section will set up the display 
screen. 

Type NEW, then 

10 POKE 36879, 8:PRINT CHRS(147) 
20 FOR L = TO 7 : FOR M = TO 7 
30 POKE 7680 + M*22 ^ L, L'S-fM 
40 NEXT : NEXT 

Now RUN the program. We now have 64 characters on the 
screen; they can't be changed in any way, since the codes for the 
letters are in ROM. If we change the character memory register to 



69 



point to RAM, however, we will be displaying memory thai we can 
change any way we want. 
Add the following line to the program: 

5 POKE 36869,255 

(This will give us 64 programmable characters, which we can set 
up as an 8-by-8 character matrix, which will give us a 64-dot by 
64-dot HI-RES screen— just what we were looking for J 

RUN the program now. Garbage appeared on the screen, 
right? Just like the regular screen, we have to clear the 
HI-RES screen before we use it. Printing a key won t work in 
this case. Instead we have to clear out the section of memory 
used for our programmable characters. Add the following 
line to your program to clear the HI-RES screen: 

6 FOR i- 7168 TO 7679 : POKE I : NEXT 

Now RUN the program again. You should see nothing but black 
on the screen— your program is a success. What we want to add 
now is the means to turn dots on and off on the HI-RES screen. 

To SET a dot (turn the dot on) or UNSET a dot you must know 
how to find the correct bit in the character memory that you must set 
to one. In other words, you have to find the character you need to 
change, the row of the character, and which bit of the row that you 
have to change. We need a formula to calculate this. 

We will use X and Y to stand for the horizontal and vertical 
position of the dot. The dot where X - and Y = is at the upper-left 
of the display. Dots to the right have higher X values, and the dots 
toward the bottom have higher Y values. 




The dots where 0< = X< = 7 and 0< = Y< = 7 are in character 
number 0, which we placed at the upper-left corner of the screen. 
Each character contains 64 dots, 8 rows of 8 dots each. 

These are the simple calculations to decide which dot of which 
character is being altered: 

The character number is. , . 
CHAR - INT{X/8} *8 + INT(Y/8) 



L 



m 



This gives the display code of the character you want to change. 
To find the proper row in the character, use this formula: 

ROW = {Y/8 - INT(Y/8)} ^S 

Therefore, the byte in which character memory dot (X,Y) is 
located is calculated by: 

BYTE ^ 7168 + CHAR*8 -r- ROW 

The last thing we have to calculate is which bit should be 
modified. This is given by: 

BIT - 7- (X- (INT(X/8)*8) 

To turn on any bit on the grid with coordinates (X, Y), use this line: 

POKE BYTE, PEEK (BYTE) OR {2 T BIT) 

Let's add these calculations to the program. In the following 
example, the VIC will plot a sine curve: 

50 FOR X - TO 63 

60 Y = INT{ 32 4- 31 * SIN (X/IO)) 

70 CH = INT(X/8}*8 ^ lNT(Y/8) 

80 RO - (Y/8 - INT(Y/8)) * 8 

90 BY = 7168 + 8*CH -r RO 

100 81 - 7- {X- INT(X/8}*8} 

110 POKE BY, PEEK(BY) OR (2 t Bl) 

120 NEXT 

130 GOTO 130 

The calculation in line 50 will change the values for the sine 
function from a range of - 1 to - 1 to a range from to 63, Lines 70 
to 100 calculate the character, row, byte, and bit being affected, 
using the formulas as given before. Line 130 freezes the program 
by putting it into an infinite loop. When you have finished looking at 
the display, just hold down RUN STOP and hit RESTORE. 

As a further example, you can modify the sine curve program to 
display a circle. Here are the lines to type to make the changes: 

55 Y1 = 32 + SQR(64*X -X^X) 

56 Y2 = 32 - SOR(64*X -X*X) 

60 FOR Y = Y1 TO Y2 STEP Y2-Y1 
125 NEXT 

This will create a circle in the HhRES area of the screen. Notice 
that the rest of the screen fills up with a stripe pattern- This Is 
because the empty spaces on the screen are filled with character 
code 32, which is normally a space — and is now one of the 



91 



programmable characters in the grid. If you didn't want the screen 
to fill up with that garbage, just fill the screen with characters with a 
code you're not using. In this case, code 160 would work nicely, 
since that points to the blank space character in ROM . Here is a line 
that cleans up the rest of the screen: 

11 FOR 1 = 7680 TO 8185 : POKE 1,160 : NEXT 

MULTI-COLOR MODE GRAPHICS 

High resolution graphics gives you controi of very smat! dots on 
the screen. Each dot in character memory can have 2 possible 
values, 1 for on and for off. When a dot is off. the dot on the screen 
is drawn with the screen color. If the dot is on. the dot is colored with 
the character color for that screen position. All the dots within each 
8x8 character can either have the screen color or the character 
color. This limits the color resolution within that space. 

Multi-color mode gives a solution to this problem . Each dot in 
multi-color mode can be one of 4 colors: screen color, character 
color, border color, or auxiliary color. The only sacrifice is in the 
horizontal resolution, because each multi-color mode dot is twice 
as wide as a high-resolution dot. This loss of resolution is more than 
compensated for by the extra abilities of multi-color mode, like the 
ability to color dots in one of 16 colors, instead of the usual 8. 

Multi-color mode is set on or off for each space on the screen, so 
that multi-color graphics can be mixed with high-resolution 
graphics. This is controlled in color memory. Color memory is in 
locations beginning at either 37888 or 38400, depending on the 
size of memory in the VIC. To find the current location of color 
memory, use the formula; 

C = 37888 - 4 * (PEEK (36866) AND 128) 

The memory in this location is a little different from that in the rest 
of the VIC. ft is wired up as nibbles instead of bytes, meaning that 
each memory location has 4 bits instead of the usual 8. When 
PEEKing values from this section of memory, the value should 
always be AN Ded with 1 5 to 'filter out" any random bits that appear 
in the upper 4 bits> 

By POKEing a number into color memory, you can change the 
color of the character in that position on the screen. POKEing a 
number from to 7 gives the normal character colors. POKEing a 
number between 8 and 1 5 puts the space into multi-color mode. In 
other words, turning the high bit on in color memory sets multi-color 
mode, and turning off the high bit in color memory sets normal (or 
high-resolution) mode, 

Once multi-color mode is set in a space, the bits in the character 

92 



r 



r 



determine which colors are displayed for the dots. For example, 
here is a picture of the letter A, and its bit pattern: 

IMAGE BIT PATTERN 



00011000 
00100100 
01000010 
01111110 
01000010 
01000010 
01000010 
00000000 



In normal or high-resolution mode, the screen color is displayed 
everywhere there is a bit, and the character coior is displayed 
where the bit is a 1 . iviulti-color mode uses the bits in pairs, like so: 



IMAGE 

AABB 

BBAA 
AA BE 

AACCCCBB 
AA BB 

AA BB 

AA BB 



BIT PATTERN 

00 01 10 00 

00 10 01 00 

01 00 00 10 
01 11 11 10 
01 00 00 10 
01 00 00 10 
01 00 00 10 
00 00 00 00 



In the image area above, the spaces marked AA are drawn 
in the border color, the spaces marked BB use the character 
color, and the spaces marked CC use the auxiliary color. The 
bit pairs determine this, according to the chart below; 



BIT PAIR 


COLOR REGISTER 


00 
01 

10 

11 


Screen color 
Border color 
Character color 
Auxiliary color 



Turn the VIC off and on, and type this demonstration program: 



100 C = 37888 + 4 * 
110 POKE 36878. 11 * 
120 PRINT CHRS(147) 
130 FOR L - TO 9 



(PEEK (36866) AND 128) 
16 : REM SET AUX COLOR 
"AAAAAAAAAA" 



93 



140 POKE C -^ L , 3 

150 NEXT 

The screen coior is white, the border color is cyan, the 
character color is black, and the auxiliary color is light cyan. 

You're not really putting coior codes in the space for screen, 
border, and auxiliary color; you're putting references to the 
registers associated with those colors. This conserves memory, 
since 2 bits can be used to pick 1 6 colors (screen and auxiliary) or 8 
colors {character and border). This also makes some neat tricks 
possible. Simply changing one of the indirect registers will change 
every dot drawn in that color. So everything drawn in the screen, 
border, or auxiliary color can be changed on the whole screen 
instantly. Here is an example using the auxiliary color: 

100 PRINT CHRS(147) CHRS(18); 

110 POKE 646 . 8 

115 FOR L = 1 TO 22 : PRINT CHR$(32); : NEXT 

120 POKE 646 , 6 

130 PRINT^'HIT A KEY" 

140 GET AS: IF A$ ="" THEN 140 

150 X = INT (RND (1) *16) 

160 POKE 36878 . X * 16 

170 GOTO 140 

There is a memory location in the VIC that is especially useful 
with multi-color mode. Location 646 is the color that is currently 
being PRINTed. When a color control key is pressed this location Is 
changed to the new color code. By POKEing this location, the 
characters to PRINT can be changed to any color, including 
multi-color characters. For exampte, type this command: 

POKE 646.10 

The word READY and anything else you type will be displayed in 
multi-color mode. Any color control will set you back to regular text, 

SUPEREXPANDER CARTRIDGE 

There is a cartridge program called the VIC SUPER EX- 
PANDER. This cartridge is programmed with many special 
functions, including a graphics package. This allows drawing of 
lines, dots, and circles, coloring in of areas on the screen, and full 
control over graphic modes. For programs in BASIC, it will be 
considerably easier to use graphics with the SUPER EXPANDER 
than by use of cumbersome pokes. The SUPER EXPANDER also 
includes 3K of extra RAM to give you enough room to do any 
high-resolution operation. 

94 



f 

L 



r 



L 



SOUND AND MUSIC 

Sound effects and music can improve almost any computer 
program, whether in BASIC or Machine Language. Obviously, a 
computer game is more exciting if you can hear the guns blazing 
and the rockets exploding. Likewise, a clever little tune provides an 
audio ^1heme^^ for a game or other program, or might become the 
"reward" if the player reaches a special '^high" score. 

Beyond games, sound effects serve other useful purposes. For 
example, a business or calculation program may be faster and 
easier to use if the computerist can enter a long string of numbers or 
formulas without looking up from a chart or balance sheet. A quick 
tone at the end of each entry indicates when a number has been 
entered ... a "buzz" might sound if the number entered has too 
many decimal places . , . and different tones might be used to 
distinguish one kind of entry from another. 

These are just a few ideas about how sound and music are used 
in computer programming. The following information is provided to 
help you the programmer understand how to use the VIC's sound 
capability to best advantage. 

FOUR "SPEAKERS" AND 5 "OCTAVES" 

The VIC has 3 tone generators (for music}, and one white noise 
generator {for sound effects). The tone generators cover 3 octaves 
each but are staggered slightly so you can actually reach a total of 5 
separate octaves. 

The VICs speakers and volume control are stored in specific 
memory locations which you can access and control by using the 
POKE command. Whenever you poke one of these locations you 
activate that tone generator, or the volume control. 

When programming sound— especially music— it is often helpful 
to think of these various sound controls as 'speakers/' and the 
volume setting as a standard Volume" control. 

Here, briefly, is a list of memory locations relating to sound: 

36878 (VOLUME SETTING) 

36874 (SPEAKER 1— MUSIC— LOWEST) 

36875 (SPEAKER 2—MUSIG— MIDDLE) 

36876 (SPEAKER 3— MUSIC— HIGHEST) 

36877 (SPEAKER 4— NOISE) 

There are 1 5 volume settings. Therefore to set the volume you must 
type the POKE command, followed by a comma, and a number 

95 



from 1 to 1 5. We suggest you always use 1 5, unless you re playing 
with the amplitude as part of a sound effect. 

Each speaker has a range of 1 28 separate settings. To 'play'' a 
particular note you must POKE one of the speaker settings, which 
happen to be numbered from 128 to 255. l\ you POKE a number 
lower than 128 or higher than 255 you will get no sound (which 
suggests one way to interrupt a speaker while its 'on"). 
Here's an example of how to play a note on the VIC: 



20 POKE 36878,15 

30 POKE 36875,200 

40 FOR X-1TO1000: 

NEXT 

50 POKE 36878.0 

RUN 



SETS VOLUME AT MAXIMUM 
TURNS ON SPEAKER NUMBER 2 
THIS IS A 1 000 COUNTTIME DELAY 

THIS TURNS THE SPEAKER OFF 
AFTER COUNTING TO 1000 



The VIC uses the television speaker as its "voice, ' so the volume 
can also be adjusted by turning the television speaker {or other 
external amplifier). 



[ 
r 



ABBREVIATING THE SOUND COMMANDS 

You can abbreviate the lengthy POKE commands described above 
by converting these to programming 'shorthand. One way is 
shown below: 

10 V -36878:51 =36874:52 = 36875:83 -36876:S4 = 36877 

Now if you want to turn on a particular speaker, or set volume, you 
can use the abbreviations . . , like, for instance: 

20 P0KEV.15 

30 POKES2.200 

40 FORX = 1TO1000:NEXT 

50 POKEV,0 




VICTIP 

In line 10, we put atl the commands on one line instead of five 
lines because we want to demonstrate economical program- 
ming techniques. You can save memory and 'crunch' longer 
programs Into less space if you put several commands on a 
single line, with each command separated by a colon, as 
shown. 



96 



r 



r 
r 



Keep this ''programming shortiiand" in mind as we show you some 
more examples of using the VIC speakers, 

HOW MUSIC WORKS ON THE VIC 

As already mentioned, the VIC^s speal<ers each cover 3 octaves, 
but together reach a total range of 5 octaves. This is because the 
VIC'S 3 tone generators are 'staggered" so the octaves of the 
different speakers overlap. A more graphic picture of which 
speakers cover which octaves and how they overlap is shown in the 
chart on page 99. Musical note values are shown below: 

TABLE OF MUSICAL NOTES 



APPROX. 




APPROX. 




NOTE 


VALUE 


NOTE 


VALUE 


C 


135 


G 


215 


C# 


143 


G# 


217 


D 


147 


A 


219 


D# 


151 


A# 


221 


E 


159 


B 


223 


F 


163 


C 


225 


F# 


167 


C# 


227 


G 


175 


D 


228 


G# 


179 


D# 


229 1 


A 


183 


E 


231 


A# 


187 


F 


232 



SPEAKER 
COMMANDS: 



WHERE X CAN BE: 



POKE 36878,X 



to 15 



POKE 36874,X 



128 to 255 



FUNCTION: 



sets volume 



plays tone 



POKE 36875,X 



126 to 255 



plays tone 



POKE 36876,X 



128 to 255 



plays tone 



POKE 36877,X 



128 to 255 



plays "noise" 



97 



The Octave Chart illustrates the three octaves contained in each 
speaker register. It also shows how several octaves overlap . . . tor 
instance, the lowest octave ot Speaker 3 contains the same notes 
as the middle octave of Speaker 2. Of course, the same note played 
on different speakers may sound slightly different . . . just as the 
same note played on a piano may sound different from the same 
note played on a harpsichord. Also, some television sets and 
speakers may cause varying results in terms of tonal qualities. 
The Table of Musical Notes on page 97 is intended to help you 
approximate note values in your computer program using the VIC. 
The number values are approximate only and may be adjusted by 
using values between those shown. 

MUSIC PROGRAMMING TECHNIQUES 

There are four basic parameters in programming music: 

1. Volume 

2. Speaker/Sound Register Selection 

3. Note 

4. Duration 

In other words, the things you have to consider when programming 
music are which volume to set, which speaker(s) to use, the notes 
being played by each speaker, and the duration of each note. Lets 
consider some techniques for putting these parameters in your 
program: 

EXAMPLE 1 : MUSIC USING DATA STATEMENT 

10 POKE 36878, 15 Set volume to highest level (15). 

20 S2 = 36875 Set speaker to equal 82 (any variabfe). 

30 READ N,D Read duration & note from DATA 

below. 

40 IF N = - 1 THEN Turn off speaker & end program at - 1 . 
POKES2.0:END 

50 POKE 82, N Play note N from DATA on Speaker 32. 

60 FORT = 1TOD: Duration loop to set up time value. 
NEXT 

70 GOTO30 Keeps going back to DATA list to get 

duration & note (N,D) values. 

80 DATA 225,250,226, DATA statements ... the first number 
250,227,250,228, is the note from the note value chart 

250,229,250,230, earlier in text, and the second 

250,231,250,232, number ts the duration the note is 

250,233,250,234, played. 



250,235,250,-1, 
'1. 



r 



98 



[ 



VIC OCTAVE COMPARISON CHART 


S3 (36876) 










D 










E C 










F T 






HIGHEST OCTAVE 


G A 










A V 










B E 










C 3 


82 (36875) 






D 


D 









E C 


E 









F T 


F 


T 






G A 


G 


A 






A V 


A 


V 






B E 


B 


E 






C 2 
D 


C 


3 


SI (36874) 
D 


D 





E C 


E 


C 


£ 


C 


F T 


F 


T 


F 


T 


G A 


G 


A 


G 


A 


A V 


A 


V 


A 


V 


B E 


B 


E 


B 


E 


C 1 


C 


2 


C 


3 


D 


O 


D 







E 


C 


E 


C 




F 


T 


F 


T 




G 


A 


G 


A 1 




A 


V 


A 


V 




B 


E 


B 


E 




C 


1 


C 

D 
E 
F 


2 



C 

T 


LOWEST OCTAVE 






G 
A 
B 
C 


A 
V 

E 
1 



99 



Pay special attention to the fact that N,D is actually each PAIR of 
values in the DATA statement in Hne 80. For example. 225,250 is 
the note (225) and time duration (250 jiffies) the note is played. The 
next note is 226 which is also held for a duration of 250, and so on 
until the computer comes to the DATA pair of - 1 . - 1 , which is the 
signal to END the program. There are two ways to end the music 
portion of a program. To turn off the music and continue the 
program you should simply POKE the speaker(s) with a zero to turn 
them off when the program reaches your DATA signal {the signal 
here is -1,-1 but it could be any number). To end the entire 
program when the music stops POKE the speaker(s) off with a 
zero and END the program by using the END command as shown 
in line 40. 



L 



EXAMPLE 2: MUSIC USfNG MULTIPLE SPEAKERS 



10 POKE 36878,15 

20 31=36874:32 = 36875: 

33 = 36876 
30 READ D,N1,N2,N3 
40 IF D=-1THENP0KES1. 

0:POKE32.0:POKE33. 

0:END 
50 POKE S1,N1 
60 POKE S2,N2 
70 POKE S3,N3 
80 F0RT = 1T0D:NEXT 
90 GOTO30 
1 00DATA500,225, 225,225, 

0,0,0,0,500,225.225,225, 

500,232232,232,0,0,0,0, 

500,232,232,232 
1 1 0DATA250, 240,240, 240, 

250,239,239,239, 

125,237,237.237, 

67.5,235,235.235, 

33,232,232,232 
120DATA33,231,231,231, 

33,228,228,228, 

33,225,225,225, 

33,223.223.223, 

500.195,195,195 
1 30DATA500,240, 240,240 
140DATA- 1,0,0,0 



This program is essentiafly the 
same as Example 1, except 
several speakers are used, and 
each speaker must be desig- 
nated separately. If you're not 
familiar with DATA statements, 
here's a good example of how 
they work. In the previous ex- 
ample we told the VIC to scan 
through the DATA and READ 
N,D where the first number was 
the NOTE and the second num- 
ber was the DURATION. In this 
example, we are rearranging 
the program so the VIC reads 
the DATA numbers in a slightly 
different order. Line 30 instructs 
the VIC to READ the data in this 
order: DURATION, NOTE 1, 
NOTE 2, NOTE 3. These notes 
are only played on their corre- 
sponding speakers per lines 
60-70. The music is the same as 
EXAfVlPLE 1 except we're using 
three speakers and setting the 
same duration for all 3 notes so 
they pfay simultaneousfy. If you 
want different speakers to play 



[' 



r 



100 



different durations, (as in most 
songs) you would change fine 
30 to: READ D1 ,N1 ,D2.N2,D3r 
N3 and put matching dura- 
tion^note pairs in the DATA 
statements. This is how you 
achieve 3-voice harmony. 

Notice also in Example 2 that the simple tune is played with all 
similar notes, then speeded up by shortening the duration values. 
The duration values may be any number, including decimal 
numbers like the "67.5" in line 1 1 which is included as an example. 
The note values may be any number between 128 and 255, with 
notes corresponding to the note value chart earlier in this chapter. 

Another parameter which you might consider changing is 
volume. An example of how volume may be changed is found in the 
"OCEAN WAVES" sound effect program on page 1 37 of the VIC 20 
Personal Computer Guide {owners manual). 

If you want to get even more sophisticated in your music/sound 
effects programming, try frequency modulation, which entails 
rapidly switching back and forth between two notes to achieve the 
illusion of a "middle" note between the two values. Example 3 
iflustrates this technique and plays a "true" scale. 

EXAMPLE 3; TRUE NOTE SCALE USiNG FREQUENCY 

MODULATION 

READY. 
MUSE 

READY. 

90 81-36674:82 = 51 -^1:83 = 81 -2:V-81 *4 

100 Dlf^N(37,1):FORI = 0TO37:READN(l.0).N(L1):NEXT 

200 FORI-^0TO37:POKEV,l5:FORJ = 0TO49:POKES1,N(l,0); 
POKES1,N(I,1):NEXTJ:POKEV=0:NEXTI 

9000 DATA1 31 ,131, 140, 140, 145, 145, 151, 151, 158,158, 161, 162, 
166.167,173,174,178,178,181,182 

9010 DATA1 85 J 86, 189. 190. 192 J 95, 197 J 97,200,200.203,203, 
206,207,203,209,211,212,214,214 

9020 DATA21 6,21 6,21 8,21 9.220.221 ,222,223,224.224.226,220, 
227,228,299,229,231 ,231 ,232,232 

9030 DAT A233,233, 234,235,235,236,237,237,237,238,239,239. 
239,240,240,241 
READY. 



101 



MUSICAL NOTE VALUES 



The accompanying chart shows the two values to modulate 
between to get the "true'^ note in the first column. Using the 
program in Example 3 above, POKE the first value, then the second 
value in line 100 to get the "true" modulated tone. 



NOTE 


VALUE 1 


VALUE 2 


C 


131 




C# 


140 




D 


145 




D# 


151 




E 


158 




F 


161 


162 


F# 


166 


167 


G 


173 


174 


G# 


178 




A 


181 


182 


A# 


185 


186 


B 


189 


190 


G 


192 


195 


C# 


197 




D 


200 




D# 


203 




E 


206 


207 


F 


208 


209 


F# 


211 


212 


G 


214 




G# 


216 




A 


218 


219 


A# 


220 


221 


B 


222 


223 


C 


224 




c# 


226 




D 


227 


228 


D# 


229 




E 


231 




F 


232 




F# 


233 




G 


234 


235 


G# 


238 


236 


A 


237 




A# 


237 


238 


B 


239 





r 



102 



c 
c# 



239 

240 



240 
241 



If two note values are given, vary the sound register on the VIC 
between those two values. If only one value is given, don't vary the 
register (just POKE the value in twice). 



^^ 
VIC TfP: Here are a few additional comments about using 
DATA statements in your programs. In Example 2 (page 100) 
we show each note set (duration, note 1 , 2 and 3) on a separate 
line to emphasize that the notes are arranged and played 
simultaneousiy * , . when you enter this DATA in your 
computer, however, you should not break up the segments 
but instead type all the numbers and commas without any 
spaces between the characters. Typing programs without 
spaces is a good way to conserve memory and reduces the 
possibility of error. 



EXAMPLE 4: THE VIC PiANO 

Finally, to give you a more familiar representation of how music 
works on the VIC, here's a program which converts the VIC 
keyboard to a "piano/' 



10 REM STORE SOUND REGISTERS 
20 82-36675 
30 V =36878 
40 POKE S2,0 

100 REM STORE B MAJOR SCALE 
110 FOR N-1 TO 8 
120 READ A (N) 
130 NEXT N 

140 DATA 223, 227, 230 
150 DATA 231, 234, 236 
160 DATA 238, 239 

200 REM PLAY KEYBOARD 
210 POKE V, 15 

220 GET AS: IF AS = "'^ THEN 220 
230 N = VAL (AS) 



Abbreviates 
the voice 
registers well 
need and turns 
them off 

Reads B major 
scaie from lines, 
140-160 

Contains POKE^ 
values for B 
major scale 

Turns on the volume^ 

Finds out what key^ 
is being pressed 



103 



240 IF N = OR N = 9 THEN 300 

250 POKE S2,0 

260 FOR T=1 TO 25: NEXT T 

270 POKE S2, A (N) 
280 GOTO 220 

300 REM ENDING MODULE 
310 POKE S2. 



Ends the program ' 
if youVe pressed 

Brief sifent intervar 
between notes. . . 

Plays the tone 
and returns to 
look for another ' 

Turn off the sound^ 
before you go 



Now, when you type RUN (and press RETURN), you can play 
tunes on your VIC. The keys in the top row with numbers on them 
control the various notes: 



1 


2 


3 


4 


5 


6 


7 


8 


DO 


RE 


Ml 


FA 


SOL 


LA 


Tl 


DO 



The VIC will keep playing the note you hit last until you hit another 
note. When you're done, press either or 9, and it will turn off. To 
start the VIC piano again, just reRUN the program. 



Try the lollowing: 










1 1 5 5 6 6 


5 


OR: 






4 4 3 3 2 2 


1 








5 5 4 4 3 3 


2 


3 3 4 5 


5 4 


3 


5 5 4 4 3 3 


2 


112 3 


3 2 


2 


115 5 6 6 


5 


3 3 4 5 


5 4 


3 


4 4 3 3 2 2 


1 8 


112 3 


2 1 


1 



L 



THE WHITE NOISE GENERATOR 

One of the "speakers" weVe ignored thus far is the White Noise 
Generator, or Speaker 4, This fourth speaker produces a blank 
noise sound like that on your television set when you fall asleep late 
at night. It has the same 3 octave range as the tone generators 
described above, and is used primarily for sound effects, either by 
itself or in conjunction with the other speakers. The combination of 
white noise and tones can produce some stunning effects. Twenty 
or so sample sound effects are listed in the VIC 20 owner's manual 
(PERSONAL COMPUTING ON THE VIC 20). 



L 



)04 



To try out the White Noise generator, try typing this: 

10 POKE36878,15 (if you don't have vol on yet) 

20 POKE36877,240 

30 FORT=1TO1000:NEXT 

50 POKE36877,0 




VIC TIP: 

If you turn a particular speaker on it will STAY ON UNTIL YOU 
TURN IT OFF. Example: POKE 36875,200 turns Speaker 2 on 
with a high-pitched tone. You must POKE 36875,0 to turn the 
speaker off. Just POKEing the Volume to zero will not turn the 
speaker off. For example, turn the volume on (POKE36878j1 5) 
then POKE a speaker on. Now turn the volume off 
(POKE36878,0) then turn it on again (POKE36878,1 5). The tone 
comes back on again automatically, right? That's because it 
was never turned off. Just the volume was turned to zero. It*s 
like a radio set on the same station. Whenever you torn the 
volume up the same station comes on. You have to turn the 
speaker to a different tone, turn it off by pokeing zero, or poke 
it to a number outside of its range to get a "silent" reading 
(under 128). 



MIXING SOUND AND GRAPHICS 

Ninety percent of the programs being written with sound or music 
will combine graphics with sound effects, so here are 3 sample 
programs which combine graphics and sound: 

EXAMPLE 1: CARD GRAPHICS 

10 A=97: B = 20: C = 122: D = 115 

20 POKE36878,15: 52 = 36875 

30 POKES2,200:PRINTCHRS(A}; 

40 FORI=1TO100;NEXTI 

50 POKES2,205:PRINTCHR$(B); 

60 FORI=1TO100:NEXTI 

70 POKES2,210:PRINTCHR$(C); 

80 FORt = 1TO100:NEXTI 

90 POKES2,215:PRINTCHRS(D); 

100 FORI = 1TO100:NEXTI 

110GOTO30 



105 



EXAMPLE 2: CALCULATING FORMULA WITH BLIPS 

10 POKE36878.15:PRINrENTER FIRST NUMBER":INPUTA I 

20 FORX = 200TO120STEP-2:POKE36875.X:NEXT 1 

30 PRINT'ENTER SECOND NUMBER":INPUTB 

40 FORX = 200T0 1 20STEP - 2:POKE36875,X:N EXT 

50 FORT = 1TO200:NEXT 

60 PRINTA"MULTIPLIED TIMES" B 

'■ = "A*B:FORX= 150TO250STEP2:POKE36875, 

X:NEXT:POKE36875,0 

70 FORT = 1 TO5000 :N EXT: GOTOI 

EXAMPLE 3: MUSICAL KEYBOARD 



10 POKE36878.15:X==128 

20 IFX>255THENGOTO10 

30 GETA$:IFA$ = ""THENGOT030 

40 PRINTAS; 

50 POKE36876,X 

60 X = X + 5;GOTO20 



L 



106 



L 



MACHINE LANGUAGE 
PROGRAMMING GUIDE 



System Overview 
Introduction to Machine Lan- 
guage 

Writing Your First Program 
Special Tips for Beginners 
Memory Maps 
Useful Memory Locations 

• The KERNAL 

• KERNAL Power Up Activities 

• VIC Chips 

— 6560 Video Interface Chip 
— 6522 Versatile Interface 
Adapter 



r 
[ 
r 
[ 



[ 



[ 



r 



r 
r 
r 



SYSTEM OVERVIEW 

This chapter provides an overall functional description of the VIC 
20 and ties hardware and software operations together to give the 
programmer more of an understanding of the way VIC 20 
processes his programs within the system. 

A simplified functional block diagram of the computer is shown in 
Figure 1-t. The major system components include the micropro- 
cessor, the program-storage read-only memories (ROMs), the 
data-storage random-access memories (RAMs). the versatife 
interface devices (VIAs, 6522), the character generator chip 
(2332), and the VIC chip which provides video and sound for the 
display. 

The 6502 microprocessor is the most complex device on the 
electronics phnted circuit board. This device is primarily responsi- 
ble for controlling all computer operations. These operations are 
controlled by addressing programs in the read-only memory 
(ROM), and then interpreting and executing these sequential 
program instructions. The interpretation and execution of instruc- 
tions are accomplished during the processor's fetch and execute 
cycles. In the fetch cycle, a program instruction is "fetched" into the 
processor's instruction register. The program counter {indicates 
the location of the instruction in ROM) is counted up, ready for the 
next instruction in sequence to be fetched into the instruction 
register. In the execute cycle, the processor executes the 
instruction which performs the operation indicated. Addresses 
indicating the destination of data being transferred are derived from 
the instruction, or calculated using program data and data from the 
internal registers. 

These controls exercised by the processor are performed by 
communicating through the 16*bit address bus, the 8-bit bi-direc- 
tional data lines, and the write-enable line. The information on the 
address bus determines the destination of the data being 
transferred, the bi-directional data bus functions as a path for data 
transferred into and out of the microprocessor, and the write-enable 
line determines the direction of the data being transferred. 

Consider the microprocessors inputs and outputs. We can 
divide these into three groups. Each of these groups forms a "bus" 
which consists of a set of paraliel paths used to transfer binary 
information between the devices in the system. 

The address bus is used to carry the address generated by the 
microprocessor to the address inputs of the memory and 
input/output (10) devices. 

109 



Figure 3-1, VIC system functional block diagram. 
110 



[ 

r 



PAT a yiis 



- ^ 



> 



2IE4 IKi 4 



£114 IK E 4 



MICH SPIED SHAFTED ADDRESS BUS 2.0MHa 



« 



— S "^ it fi- y 




LOU Hi uP ADO*»eSS BUS 



TTTITTTTTTT 



jiMiYiunm I 



:> 



^ llljIMI s f^^J 

St 



^ 



g llllilil I 



r 



[ 



r 



The data bus consists of eight bi-directional data lines. During a 
write operation, these fines transfer data trom the processor to a 
memory location selected by the address tines. During a read 
operation, data is transferred from memory to the processor along 
the same fines- The data bus is, therefore, used to carry all data and 
instructions to and from the processor, memory, and the peripheral 
devices. 

To understand the operation of the control lines which comprise 
the control bus, we will examine one individually. Since the data bus 
is bi-directional, the processor must have some method of 
signalling to memory or I/O to which direction data transfer will take 
place {whether memory or the 10 is to be read or written to). This 
function is performed by the R/W (ReadA/Vrite) output from the 
processor. When this line is high, all data transfers will take place 
from memory to the processors— a read operation. If the RA/V line is 
low, then the processor will write data out to memory. 

Other control lines which comprise the controi bus are: system 
clock timing— used to time the operation of the system including 
data transfers; reset (RST) line— used to initialize the processor 
when the machine is switched on; and interrupt (IRQ and NMI) 
lines— used to cause the processor to stop its current program and 
start a new program at a specified location. 

The program memory is the storage for the sequence of BASIC 
instructions which compnse the system programs. The micropro- 
cessor fetches these instructions by placing the appropriate 
address on the address bus. In response, the memory puts the 
instruction, in the form of a pattern of 1's and O's, on the data bus. 

The program memory is called a read-only memory because the 
microprocessor cannot store information into the ROM device. 
However, by addressing the ROM, the processor can cause the 
corresponding 8 bits of data to be transferred on the data bus. The 
ROM is a nonvolatile device, i.e., data is not destroyed when power 
is disconnected from the system. 

The read-write, random-access memory (RAM) provides tem- 
porary storage for input data, arithmetic operations, and other data 
manipulations. Each RAM address corresponds to eight memory 
cells. However, when power is removed from the system, all 
RAM*stored data is lost; the RAM is therefore a volatile memory 
device. 

The versatile interface adapters provide interface for the 
keyboard, user port, control port, and the serial bus. The serial bus 
provides the communication for peripheral units, such as floppy 
disk drives, printers and etc. Each port is assigned a unique 
address to permit communication with the microprocessor (Figure 
3-3). 

lit 



L 

r 



§ 



Q 




I I I I I I I I 



££| 2 



-f ^ 



DC 

^ If. 



Figure 3-2. The 6502 microprocessor 

112 



L 



[ 



L 
L 

r 



:;! o 









i C: — 



i ^ 
s s 






ffl 



4 4 A 



o 

_l UJ 

-J ^, 

UJ 



p 



1VN>I3XNI 



HE 



O Efl QJ 






44144444 









o 



IE 



12. 



urim 



t- < F - 

^ I- w' 

a, ^ 1-, ; 



31 



HflVlVMfSlM 



IT 



3 



Wf: 



vr~r~v~v~r-n 



y 



o 
I- i^ 



F 

^ ^ a 

< s o 
>- >- at 

^ < ^ 

OD '^ *rt 

^ L/} 4 

i; 1^. y^ 

< c if 



! C ^ 



iU £K O 

a h- X 

o /: u 

Q O < 

■^ LU 



functional bfock diagram 



o 



113 



Decimal 

37136 

37887 



38912 
39936 
40959 

49152 
57344 
65535 



l/O-O 

i 




1/0-2 


1/0-3 




BASIC ROM 


Kernal ROM ' 

1 



Hex 

9110 

93FF 



9800 
9C00 
9FFF 

GOOD 
EOOO 
FFFF 



L 



Figure 3-3. VIA port assignments. 

The video interface chip (VIC) implements color video graphics 
for the system. It provides all circuitry necessary for generating 
color programmable character graphics with high resolution. VIC 
also incorporates sound effects and A/D converters to accommo- 
date video games. Its on-chip sound system includes three 
independent, programmable tone generators, a white-noise 
generator and an amplitude modulator. 

The VIC 20 character generator contains all characters used in 
the system. There are two complete character sets used; (1 } Upper 
case with full graphics, and (2) upper case and lower case with 
limited graphics. Also, each of these character sets is represented 
in its reverse mode. These characters are stored in the ASCII 6-bit 
code and are arranged in 8x8 bit cells. Also, each character is 
stored every 8 bytes in the memory. The diagram in Figure 3-4 
shows the character generator memory layout. 

VIC'S 6502 microprocessor can access up to 32,000 Indepen- 
dent user-RAM memory locations (with memory expansion). You 
can think of VIC s memory as a book with up to 256 "pages," with 
256 memory locations on each page. For example, page S80 is the 
256 memory locations beginning at location S8000 and ending at 
location 380 FF. Since the 6502 uses two 8-btt bytes to form the 
address of any memory tocation. you can think of one of the bytes 
as the page number and the other as the location within the page. 

The amount oi active RAM may be 3,58K (addresses 4096 to 

114 



L 



Decimal 

32768 



33792 



33816 



35840 



36863 



Upper case and 
graphics 


1K 


Upper case and 

graphics 

reversed 


1K 


Upper case and 
lower case 


1K 


Upper case and 
lower case 
reversed 


1K 



Hex 

8000 



8400 



8800 



8C00 



8FFF 



Figure 3-4. Character generator memory layout. 



Decimal 




1024 

4096 

7680 
8192 

16384 

24576 

32768 



36863 



Worl<ing Storage 
RAM 


IK 


Expansion RAM 


3K 


, User BASIC 
1 Program RAM 


4K 


Screen RAM 


Expansion 
RAM/ROM 


8K 


Expansion 
RAM/ROM 


8K 


Expansion 
RAM/ROM 


8K 


Ciiaracter ROM 


4K 



Hex 

0000 



0400 

1000 

1E00 
2000 

4000 

6000 

8000 

8FFF 



Figure 3-5, VIC20 memory Jocations, 
115 



Decimal 

36864 

37136 
37888 
38912 
39936 
40960 
49152 
57344 
65535 



VIC Chip 


l/O-O 


Color RAM 


1/0-2 


1/0-3 


Expansion ROM 


8KJ 

5 


BASIC ROM 

! 


8K 


KERNAL ROM 


8K 



Hex 

9000 

9110 
9400 
9800 
9CO0 
AOOO 
COOO 
EOOO 
FFFF 



Figure 3-5. (cont). 



7679), 6.65K (addresses 1 024 to 7679), or a total of 32K by adding 
24K more RAM (addresses 8192 to 37267. Addresses 40960 to 
49151 are allocated for the expansion of ROM. The first 1K-byte 
allocation (to 1024) is fixed; the larger the memory size, the more 
space is available in the user program area. 

VIC has three types of memory: random-access memory (RAM), 
read-only memory (ROM), and input/output tocations (I/O). Figure 
3-5 shows a typical VIC 20 memory, the different types, and the 
operations for which they are used. 

Each portion of the memory is described in more detail in the 
following text. 

The first 1K-byte of RAM (Addresses - 1023) )s allocated 
to working storage, the stack, and tape buffers. Byte ad- 
dresses 4096 through 8191 are allocated to screen storage 
and storage of user programs (Figure 3-6). 

Locations 256 through 51 1 are used for the stack area for BAS IC, 
KERNAL and the microprocessor. The stack begins at location 51 1 
and proceeds downward. Storage is ailocated dynamically as 
needed by BASIC and the hardware. An OUT-OF-MEMORY error 
occurs if the stack pointer reaches the end of available space in this 
area. 

Locations 512 through 827 are used as additional BASIC and 
KERNAL working-storage locations. 

Locations 828 through 1 023 form a tape buffer area for the tape 
cassette. 



116 



r 



Decimal 





144 

256 

512 

828 

1024 
4096 



7680 
8191 



BASIC 
Working Storage 



KERNAL 
Working Storage 



BASIC & KERNAL 
Stack 



BASIC & KERNAL 
Working Storage 



Tape Buffer 
Working Storage 



Expansion RAM 



User BASIC Text 



Variables 

& 
Arrays 



Strings 



Screen RAM 



Hex 

0000 



0090 

0100 

0200 

0330 

0400 
1000 



1E00 
1FFF 



Figure 3-6. Working storage and user programs « 

Locations 4096 through 7679 are used tor storage of the user 
program and variables. The program begins at location 4096 and is 
stored upward toward the end of memory. Variable storage begins 
after the end of the program. Array storage begins at the end of 
variable storage. Strings are stored beginning at the end of memory 
and working downward. An OUT-OF- MEMORY error occurs if an 
upgoing pointer meets the downgoing pointer {Figure 3-6). 

Addresses 1024 through 4095 are allocated for the expansion of 
RAM. Addresses 8192 through 32767 are allocated for the 
expansion of either RAM or ROM, up to 32K-bytes> Addresses 
40960 through 49151 are allocated for ROM expansion only 
{Figure 3-7). 



117 



Decimal 

1024 



4095 
8192 



16384 



24576 



32767 
40960 



Expansion 


RAM 


3K 




Expansion 
RAM/ROM 




8K 


Expansion 
RAM/ROM 




8K 


Expansion 
RAM/ROM 




8K 




Expansion 


ROM 


8K 



Hex 

0400 



OFFF 
2000 



4000 



6000 



7FFF 
AOOO 



49151 ' 1 BFFF 

Figure 3-7. Expansion RAiU/ROM. 

Locations 37136 tinrough 37887, and 38912 through 40959 are 
the memory-mapped I/O locations. Locattons 49152 through 
65535 comprise the BASIC interpreter and KERNAL routines 
(Figure 3-B). 



Decimal 

37136 

37887 
38912 
39936 
40959 
49152 
57344 
65535 



l/O-O 



1/0-2 



1/0-3 



BASIC ROM 



KERNAL ROM 



Hex 
9110 

93FF 

9800 

9C00 

9FFF 

COOO 

EOOO 

FFFF 



Figure 3-8. BASIC, KERNAL, and I/O locations. 

118 



L 

r 
[ 



L 

[ 

r 



r 
r 



Location 65535 is the end o1 the VIC memory. 

The VIC BASIC interpreter executes a user program by 
interpreting each source line stored in memory in its compressed 
form. First, however, a discussion about how the program is stored 
in memory is necessary. 

When a program line is entered from the keyboard, the screen 
editor takes control, allowing you to edit the line until you press the 
RETURN key. When the RETURN key is pressed, the BASIC 
interpreter performs two actions: first, the program line rs translated 
into its compressed form, that is, reserved words and fog ica! -opera- 
tor keywords are represented by their one-byte tokens; then, the 
interpreter stores the program line in memory in its ascending line 
number order. When the RETURN key is pressed, the BASIC 
interpreter searches memory for the same line number. If there is a 
line with the same number, it is replaced with the new line. If there is 
not a line with the same number, the next higher line number is 
encountered and the interpreter then inserts the new line into 
memory. 

Program lines are stored at the beginning of the user program 
area of memory which starts at memory location 4096. Variables 
are stored in memory above the program lines, and arrays are 
stored above the variables. All three areas begin at lower 
addresses and build upwards to higher addresses. Strings are 
stored beginning at the top of memory and work downwards. The 
BASIC interpreter buitds all four areas, moving them as necessary 
and adjusting pointers for insertions and deietions. Eight pairs of 
memory locations contain pointers to the division points in the user 
program area of memory. These pointers are shown in Figure 3-9. 



Pointer Address 




Typical Values 


{2B.2C) Start of Text 


BASIC 
Statements 


4097 


(41,42) DATA Statement Pointer 




5879 


(2D,2E) Start of variables 


Variables 


5016 


(2F.30) End of variables 


Arrays 


5144 


(31,32) End of arrays 




5303 


(33,34) End of strings 


Strings 


7657 


(37,38) Top of memory 




7679 



Figure 3-9. Principal pointers in user program area. 
119 



Next, we will discuss the formats in which BASIC statements, 
variables, arrays, and strings are stored in their respective areas. 

The BASIC statement storage table (Figure 3-10) shows the 
format in which BASIC statements are stored. Memory location 
4097 contains a pointer to the beginning of the first BASIC 
statement. The pointer, like all addresses in the VtC, is stored in 
low-byle, high*byte order. The pointer is a link to the memory 
address of the next link. A link address of zero denotes the end of 
the text; i.e., there are no more links and no more statements. 
BASIC statements are stored in order of ascending line numbers, 
even though there are links to the next statement. Links are used to 
quickly search through line numbers. 

The statement line number (stored in low-byte, high-byte 
order) follows the link address. Line numbers go from to 
63999 (stored as and 0, and 255 and 249 respectively). 



4097 4098 


4099 4101 


END 


Link 


Line# Compressed BASIC Text 





Link 

• 
• 

Link 



Une# Compressed BASIC Text 

Line# Compressed BASIC Text 
(End of text is indicated by 
two link bytes of zero.) 


End of 
statement 
is flagged 
by zero 
byte. 





Figure 3-10. BASIC statement storage. 



r 



After the line number, the BASIC statement text begins. 
Reserved words and logical-operator keywords are stored in a 
compressed format. A one-byte token is used to represent a 
keyword. All keywords are encoded such that the high-order bit is 
set to 1 . Other elements of the BASIC text are represented by their 
stored ASCII code. Other elements are comprised of constants, 
variable and array names, and special symbols other than 
operators and are coded just as they appear in the original BASIC 
statement. The BASIC keywords table (Table 3-1) shows the byte 
codes for ail values from to 255 that may appear in the 
compressed BASIC text. Codes are interpreted according to this 
table except after an odd number of double quotation marks 



120 



enclosing a character string; within a character string the VIC ASCII 
codes prevail. 





Table 


3*1. VIC 20 BASIC Keyword Codes 




Code 


Character/ 


Cpde 


Character/ 


Cade 


Character/ 


Code 


Character/ 


(decimal) 


Keyword 


(cfECimal) 


Keyword 


(deciTTia!! 


Keyword 


(decimal) 


Keyword 





End of Sine 


66 


B 


133 


INPUT 


169 


STEP 


1-31 


Unused 


67 


C 


134 


DIM 


170 


+ 


32 


space 


68 


D 


135 


READ 


17! 


- 


33 


! 


69 


E 


136 


LET 


172 


' 


34 


»■ 


70 


F 


137 


GOTO 


173 


/ 


35 


# 


71 


G 


138 


RUN 


174 


1 


36 


$ 


72 


H 


139 


IF 


175 


AND 


37 


% 


73 


1 


140 


RESTORE 


176 


OR 


38 


L 


74 


J 


14! 


GOSUB 


177 


> 


39 


' 


75 


K 


142 


RETURN 


178 


= 


40 


( 


76 


L 


143 


REM 


179 


< 


41 


) 


77 


M 


144 


STOP 


180 


SON 


42 


• 


78 


N 


145 


ON 


181 


INT 


43 


* 


79 





146 


WAIT 


182 


AGS 


44 


H 


80 


P 


147 


LOAD 


183 


USR 


45 


- 


81 


Q 


148 


SAVE 


184 


FRE 


46 


• 


82 


R 


149 


VERIFY 


195 


POS 


47 




83 


S 


150 


DEF 


186 


SQR 


48 





84 


T 


151 


POKE 


187 


RND 


49 


1 


85 


U 


152 


PR[NT# 


188 


LOG 


50 


2 


86 


V 


153 


PRINT 


189 


EXP 


51 


3 


87 


w 


154 


CONT 


190 


COS 


52 


4 


88 


X 


155 


LIST 


191 


SIN 


53 


5 


89 


Y 


156 


CLR 


192 


TAN 


54 


6 


90 


z 


157 


CMD 


193 


ATN 


55 


7 


91 


i 


158 


SYS 


194 


PEEK 


56 


8 


92 


\ 


t59 


OPEN 


195 


LEN 


57 


9 


93 


] 


160 


CLOSE 


196 


STR$ 


58 




94 


t 


161 


GET 


197 


VAL 


59 




95 




162 


NEW 


198 


ASC 


60 


< 


96^127 


Unus&d 


163 


TAB{ 


199 


CHRS 


61 


= 


128 


END 


m 


TO 200 


LEFTS 


62 


> 


129 


FOR 


165 


FN 


201 


RIGHTS 


63 


? 


130 


NEXT 


166 


SPC( 


202 


MIPS 


64 


^ 


131 


DATA 


167 


THEN 


203-254 


Unused 


65 


A 


132 


INPUTS 


168 


NOT 


255 


TT 



Note that the left parenthesis is stored as part of the one-byte 
token for functions TAB and SPG; however, the other functions use 
a separate byte for this symbol. For example, the line 

10 IF INT(A)<5 THEN PRINT TAB(X) 

would be coded as the following bytes (in decimal): 

121 



LINK 10 139 32 181 40 65 411179 53 32 167 32 153 32 163 88 41 


Line 
Number 


' 


{ A ) < 5 


X ) 



IF 



INT 



THEN PRINT TAB( 



r 

L 

r 



The operators ( - - V < ^ > as well as the words AND, OR, and 
NOT are given keyword codes (high-order bit set) since they 
'drive" the BASIC interpreter just as reserved words do (e,g., 179 
for <). The standard ASCIi codes for these symbols (e.g., 60 for <) 
appear only in the text of a string. 

Spaces in the source line are stored except for the space 
between the line number and first keyword. This space is supplied 
on LiSTing when a stored statement is expanded to its original 
form. You can conserve memory storage space by eliminating 
blanks (but this makes the program harder to read). You can also 
conserve space by putting more than one statement on a line, since 
the five bytes of link, line number, and end byte are stored only 
once. 

The size of each statement is variable and is terminated by a byte 
of zero to indicate the end of the statement. (An ASCII zero 
anywhere within the text is stored as 48.) Zero-byte flags are used 
by the BASIC interpreter in executing a program when it goes 
through the compressed BASIC text from left to right picking out 
keywords and performing the indicated operations, A zero byte 
indicates the end of the statement; the next four bytes are the link 
to the line number of the next statement. Instead of search- 
ing through the text and using byte indicators to locate the 
next statement, links are used when searching the state- 
ments for their line numbers. Three consecutive bytes of 
zero (the last statement's byte followed by two zero link 
bytes) flag the end of text when executing the program. 

A program stored onto cassette tape is in the same format as 
shown in Figure 3-10 for memory storage. Therefore, it is basically 
"dumped" onto tape in a continuous block, including link addresses 
and end bytes. 

The use of tokens in place of keywords is not unique to the VIC, 
but there ts no standard coding from one interpreter to another. 
Thus, a BASiC source program SAVEd on tape by VIC BASIC is 
not compatible with other BASiCs, nor can BASIC programs 
generated on other (non-CBM) machines normally be loaded by 
the VIC BASiC interpreter. 



r 



r 



122 



INTRODUCTION TO MACHINE 
LANGUAGE 

WHAT IS MACHINE LANGUAGE? 

At the heart of every microcomputer, there is a central 
microprocessor, a very special microchip which is the "brain'" of the 
computer. The VIC 20 ^s microprocessor is the 6502 chip. Every 
microprocessor understands its own language of instructions, and 
these instructions are called the machine language instructions of 
that chip. To put it more precisely, machine language is the ONLY 
programming language that your VIC 20 really understands. It is 
the native language of the machine. 

If machine (anguage is the only language that the VfC 20 
understands, then how does it understand the VIC BASIC 
programming language? If VIC BASIC is not the machine language 
of the VIC 20, what makes the VIC 20 understand VIC BASIC 
instructions such as PRINT and GOTO? 

To answer this question, we must first see what happens to your 
VIC 20 when you turn it on. How does your computer know what to 
do when it is first turned on? Welf, apart from the microprocessor 
which is the brain of the VIC 20, there is a huge machine language 
program which is "burnt" into a special type of memory called HOhA 
that cannot be changed, and does not get erased when the VIC 20 
is turned off, unlike a program that you put into the VIC's RAM. This 
huge program is in two parts, one taking care of the BASIC 
language, and the other called the "operating system." 

The operating system is in charge of "organizing" all the memory 
in your machine for various tasks, looks at what characters you type 
on the keyboard and puts them onto the screen, and a whole 
number of other functions. The operating system can be thought of 
as the "intelligence and personality" of the VIC 20 (or any computer 
for that matter). So when you turn on your VIC 20, the operating 
system takes control of your machine, and after it has done its 
housework, it then says: 

READY. 



The operating system of the VIC 20 then allows you to type on the 
keyboard, and use the built-in 'screen editor" on the VIC 20, The 
screen editor allows you to move the cursor, DELete, INSert, etc, 
and is, in fact, only one part of the operating system that is built-in 
for your convenience. 

123 



AH of the commands that are available in VIC BASIC are simply 
recognized by another huge machine language program built into 
your VIC 20. This huge program 'RUN'^ ^s the appropriate piece of 
machine language depending on which VIC BASIC command is 
being executed. This program is called the 'BASIC interpreter," 
because it interprets each command, one by one, unless it 
encounters a command it does not understand, and then the 
familiar message appears: 

7SYNTAX 

ERROR 

READY. 



WHAT DOES MACHINE CODE LOOK LIKE? 

You should be familiar with the PEEK, and POKE commands in 
the CBM BASIC language for changing memory locations. You will 
probably have used them for graphics on the screen, and for sound 
effects. The memory locations will have been 36874, 36875, 
36876, 36877, 36878 for sound effects. This memory location 
number is known as the "address^' of a memory location. If you can 
imagine the memory in the VIC 20 as a street of houses, the number 
on the door is, of course, the address. Now we will look at which 
parts of the street are used for which purpose. 

SIMPLE MEMORY MAP OF THE VIC 20 

Address Description 

Start of memory. 

to Memory used by BASIC and the operating system. 
1023 

1024 

to This is a gap in memory for a 3K memory expansion 

4095 module 

4096 

This is YOUR memory. This is where your BASIC or 
machine language programs, or both, are stored. 
This is also where the screen memory would begin 
on a VIC 20 that has at least 8K of RAM expansion 
memory, in which case the screen BAM that 
follows would become user memory space, 

7679 continuing up to the top of the expansion memory. 



to 



r 

L 



L 



L 

r 



124 



7680 

to 
8185 

to 
32768 

to 
36863 

36864 

to 
36879 

37136 

to 
37167 

37888 

to 
38399 

38400 

to 
38911 

38912 

to 
40959 

40960 

to 
49151 

49152 

to 
57343 

57344 

to 
65535 



This is the screen memory. 

This is a gap in memory for memory expansion. 

Character representations. 

The VIC chip registers. 

Input and output chip registers. 

Character color control table in expanded VIC 20 

Character color control table. 



Unused. 



Expansion ROM. 



8K VIC BASIC Interpreter. 



8K VIC KERNAL OPERATING SYSTEM 



Don't worry if you don't understand what the description of each 
part of memory means. This will become clear from other parts of 
this manual. 

Machine language programs consist of instructions which may or 
may not have operands (parameters) associated with them. Each 
instruction takes up one memory location, and any operand will be 
contained in one or two locations following the instruction. 

In your BASiC programs, words like PRINT, and GOTO do, in 
fact, only take up one memory location, rather than one for each 
character of the word . The contents of the I ocation that represents a 
particular BASIC keyword is called a "token/' In machine language, 



125 



there are different tokens for different instructions, which also take 
up just one byte (memory iocation = byte). 

Machine language instructions are very simple, i.e., each 
individual instruction cannot achieve a great deal. Machine 
language instructions either change the contents of a memory 
location, or change one of the internal registers (special storage 
locations) inside the microprocessor. The internai registers form 
the very basis of machine language. 

REGISTERS INSIDE THE 6502 
MICROPROCESSOR 

THE ACCU MULATOR— This is THE most important register in the 
microprocessor. Various machine language instructions allow you 
to copy the contents of a memory location into the accumulator, or 
copy the contents of the accumulator into a memory location, or 
modify the contents of the accumulator or some other register 
directly, without affecting any memory. Also, the accumulator is the 
only register that has instructions to perform math on it. 

THE X INDEX REGISTER — ^There are instructions to do nearly all 
of the transformations you can do to the accumulator, and other 
instructions to do things that only the X register can do. Again, 
various machine language instructions allow you to copy the 
contents of a memory location into the X register, or copy the 
contents of the X register into a memory location, or modify the 
contents of the X, or some other register directly, without affecting 
any memory. 

THE Y INDEX REGISTER— There are instructions to do nearly all 
of the transformations you can do to the accumulator^ and the X 
register, and other instructions to do things that only the Y register 
can do. Again, various machine language instructions allow you to 
copy the contents of a memory location into the Y register, or copy 
the contents of the Y register into a memory location, or modify the 
contents of the Y, or some other register directly, without affecting 
any memory. 

THE STATUS REGISTER— This register consists of eight "flags" 
(a flag = something that indicates that something has, or has not, 
occurred}. 

THE PROGRAM COUNTER— This contains the address of the 
current machine language instruction being executed. Since the 

126 



f 

L 
[ 



\ 



operating system is aiways "RU Mining in the VIC 20 {or. for tl^at 
matter, any computer), the program counter is always changing. It 
could only be stopped by halting the microprocessor in some way. 

The Stack Pointer— This register contains the location of the first 
empty place on the stack. The stack is used for temporary storage 
by machine language programs, and by the computer 

THE TOOLS AVAILABLE; GETTING 
READY. , , 

How Can You Write Machine Language Programs? 

Since machine language programs reside in memory, and there 
is no facifity in your VIC 20 for writing and editing machine language 
programs, you must use either a program to do this, or write for 
yourself a BASIC program that "allows" you to write machine 
ianguage. 

Most commonly used to write machine language programs are 
"assemblers. ■ These packages allow you to write machine 
language instructions in a standardized "mnemonic" format, which 
makes the machine language program a good deal more readable 
than a stream of numbers. To recap: A program that allows you to 
write machine language programs in mnemonic format is called an 
"assembler, ' and also, a program that displays a machine 
language program in mnemonic format is called a "disassembler." 
Available for your VIC 20 is a machine language monitor cartridge 
(with assembler disassembler, etc.) made by Commodore. 

VlCMon 

The VlCMon cartridge available from your local dealer is a 
program that allows you to escape from the world of VIC BASIC, 
into the land of machine language. It can display the contents of the 
internal registers in the 6502 microprocessor, and it allows you to 
display portions of memory, and change them on the screen, using 
the screen editor. It also has a built-in assembler and disassembler, 
and many other features that allow you to write and edit machine 
language programs easily. 

You don't HAVE to use an assembler to write machine language, 
but the task is considerably easier with it \i you wish to write 
machine language programs, it ts advised strongly that you buy an 
assembler of some sort. Without an assembler you will probably 
have to "POKE" the machine language program into memory, 
which, if you value your sanity, is totally inadvisable. This manual 

127 



will give examples in the format that VICMon uses from now on. 
Nearly all assembler formats are the same, therefore the machine 
language examples shown will almost certainly be compatible with 
any assembler other than the one incorporated in VICMon. 

Hexadecimal Notation 

This is a notation which most machine language programmers 
refer to when referring to a number or address m a machine 
language program. 

Some assemblers let you refer to addresses and numbers In 
decimal (base 1 0), binary (base 2), or even octal (base 8) as well as 
hexadecimal (or just "hex" as most people say). These assemblers 
do the conversions for you. 

Hexadecimal will probably seem a little hard to grasp at first, but 
like most things it doesn^t take long (with practice) to master it. 

By looking at decimal (base 10) numbers, you will see that each 
digit in that number ranges between zero and a number equal to the 
base less one, Le., > 9. THIS IS TRUE OF ALL NUMBER BASES. 
Binary {base 2} numbers have digits ranging from zero to one 
(which is one less than the base). Similarly hexadecimal numbers 
should have digits ranging from zero to fifteen, but we do not have 
any single digit figures for the numbers ten to fifteen, so the first six 
letters of the alphabet are used instead: 

BINARY 

— 00000000 
00000001 

— 00000010 
0000001 1 

— 000001 00 
00000101 

— 00000110 
00000111 

— O0001000 
00001001 

— OOOOIOIO 
00001011 

— 00001100 
00001101 

— 00001 110 
00001 1 1 1 

— 0001 0000 

If that's confusing, let's try to look at it another way: 



128 



DECIMAL 


HEXADECIMAL 





— 





1 


— 


1 


2 


— 


2 


3 


— 


3 


4 


— 


4 


5 


— 


5 


6 


— 


6 


7 


■ — 


7 


8 


— 


S 


9 


— 


B 


10 


— 


A 


11 


— 


B 


12 


— 


C 


13 


— 


D 


14 


— 


E 


15 


— 


F 


16 


— 


10 


etc. 







I 

L 

r 



r 



Example of how a 

Base raised by 
increasing powers. 

Equals: 

Consider 4569 {bas 


base 10 (deci 

3 2 1 
.10 10 10 


mal 


10 


number) is constructed. 

1000 + 5x100 + 6x10 + 9 


..1000 100 10 


1 


ie10)4 5 69 = 


4x 



Example of how a base 16 (hexadecimal number) Is 
constructed. 

Base raised by 3 2 10 

increasing powers., 16 16 16 16 



Equals:. 4096 256 16 1 



Consider 11D9 (basal 6) 1 1 D 9 = r4Q96-r 1*256^13*16^9 



Therefore 4569{Base10) = 11D9(Base16) 

The range for addressable memory locations is - 65535 (as 
was stated earlier). This range is therefore - FFFF in 
hexadecimal notation. 

Usually hexadecimal numbers are prefixed with a dollar sign, to 
distinguish them from decimal numbers. Let's look at some "hex" 
numbers, using VICMon by displaying the contents of some 
memory. VICMon shows you: 

B' 

PC SR AC XR YR SP 
.; 0401 32 04 5 E 00 F6 (these may be different) 

Then if you type in: 

.M 0000 0020 {and press RETURN ). 

You will see rows of 6 hex numbers. The first 4 digit one is the 
address of the first byte of memory being shown in that row, and the 
other five numbers are the actual contents of the memory locations 
beginning at that start address. 

You should endeavor to learn to "think" in hexadecimal. This is 
not difficult, since there is no need to think in decimal. For example, 
if tt is said that a particular value is stored at $14ED instead of 5357, 
this shoufdn't cause any headaches. 



129 



YOUR FIRST MACHINE LANGUAGE 
INSTRUCTION 

"LDA" — Load the Accumulator 

In 6502 assembly language, mnemonics are always three 
characters. LDA represents 'load accumulator with. . /', and what 
the accumulator should be loaded with is decided by the 
parameter(s) associated with that instruction. The assembler 
knows which token is represented by each mnemonic, and when it 
''assembles" an instruction, it simply puts into memory (at whatever 
address has been specified), the token and what parameters are 
given. Some assemblers give error messages, or warnings when 
the user has tried to assemble something that either the assembler 
or the 6502 microprocessor cannot do. 

If we put a "#" symbol in front of the parameter associated with 
the instruction, this means that we wish the register specified in the 
instruction to be loaded with the "value" after the "#'\ For 
example: — 

LDA #305 

This instruction wiil put 505 (decimal 5) into the accumulator 
register. The assembler will put into the specified address for this 
instruction, SA9 (which is the token for this particular instruction, in 
this mode), and it will put S05 into the next location after the location 
containing the instruction (SA9). 

If the parameter to be used by an instruction has "#"^ before it, 
i.e., the parameter is a "value." rather than the contents of a 
memory location, or another register, the instruction is said to be in 
the "immediate" mode. To put this into perspective, let us compare 
this with another mode. 

If we want to put the contents of memory location St02E into the 
accumulator, we are using the "absolute" mode of instruction: 

LDA S102E 

The assembler can distinguish between the two different modes 
because the latter does not have a "#" before the parameter. The 
6502 microprocessor can distinguish between the immediate 
mode and the absoiute mode of the LDA instruction because they 
have slightly different tokens. LDA (immediate) has SA9 (as stated 
previously), and LDA (absolute) has SAD. 

The mnemonic representing an instruction usually implies what it 
does. For instance, if we consider another instruction, "LDX, ' what 
do you think this does? 

If you said "load the X register with. . :\ go to the top of the class. 

130 



[ 



If you didn t, then don't worry; learning machine language does take 
patience, and cannot be accomplished in a day. 

The various internal registers can be thought of as special 
memory locations, because they too can hold one byte of 
information. It is not necessary for us to explain the binary 
numbering system (base 2) since it follows the same rules as 
outlined for hexadecimal and decimai outlined previously, but one 
^'bit*' is one binary digit and eight bits make up one byte. 

The maximum number that can be contained in a byte is the 
largest number that an eight digit binary number can be. This 
number is 11111111 (binary), which equals SFF (hexadecimal), 
which equals 255 (decimai). You have probabiy wondered why only 
numbers from zero to two hundred and tifty-five could be put into a 
memory location. If you tr/ POKE 7680,260 (which is a BASIC 
statement that "says":— "Put the number two hundred and sixty 
into memory location seven thousand, six hundred and eighty," 
The BASIC interpreter knows that only numbers - 255 can be put 
in a memory location, and your VIC 20 will reply with: 

7ILLEGAL QUANTITY 

ERROR 
READY. 



If the limit of one byte is SFF (hex), how is the address parameter 
in the absolute instruction "LDA S102E" expressed in memory? 

Well, it is expressed in two bytes (it won^t fit into one, of course). 
The lower (rightmost) two digits of the hexadecimal address form 
the 'low byte" of the address, and the upper (leftmost) two digits 
form the "high byte/' 

The 6502 requires any address to be specified with its iow byte 
first, and then the high byte. This means that the instruction "LDA 
SI 02E" is represented in memory by the three consecutive values: 

SAD, $2E, S10 

We need to know one more instruction before we can write our 
first program. That instruction is 'BRK." For a full explanation of this 
instruction, refer to M.O.S 6502 Programming Manual, You can 
think of it as the "END" instruction in machine language. 

If we write a program with VICMon and put the BRK instruction at 
the end, the program will return to VICMon when it is finished. This 
might not happen if there is a mistake in your program, or if the BRK 
instruction is never reached (just like an ''END" statement in BASIC 
may never get executed, and thus if the VIC 20 didn't have a STOP 
key, you wouldn't be able to abort your BASIC programs!) 



131 



WRITING YOUR FIRST PROGRAM 



If you have used the POKE statement in BASIC to put characters 
onto the screen, you will be aware that the character codes for 
POKEIng are different to CBM ASCII character values. For 
example, if you enter: 

PRINT ASCC^A") (and press <RETURN> ) 

The VIC 20 will respond with: 

65 

READY. 

* 

However, to put an ^^A'^ onto the screen by POKEing, the code Is 
1 . Since the screen memory starts at 7680 (decimal), or 4096 if you 
have 8K or more of expansion memory, by entering: 

<CLR> (To clear the screen) 

POKE 7680,1 (and<RETURN> ) (NOTE: POKE 4096,1 on a 

VIC 20 

with 8K or more of 
expansion memory) 

The "P" in the POKE statement should now be an "A," We will 
now do this in machine language. Type the following in VICMon: 

(Your cursor should be flashing alongside a '\ " right now.) 
A 1400 LDA #$01 {and press <RETURN> ) 

The VIC will prompt you with: 

.A 1402* 

Type: 

.A 1402 STA S1E00 (or STA S100O on a VIC ,20 

with SK or more of expansion memory) 

The STA instruction stores the contents of the accumulator in a 
specified memory location. The VIC will now prompt you with: 

,A 1405 * 
Now enter: 

.A 1405 BRK 
Clear the screen, and type: 

G 1400 

132 



r 



I 
f 

L 

r 



L 

r 



The G should turn into an "A" if you have done eveiTthing 
correctly. You have now written your first machine language 
program! Its purpose is to store one character, the letter A, in the 
first byte of screen memory. 

ADDRESSING MODES 

ZERO PAGE 

As shown earlier, absolute addresses are expressed in terms of 
a high order and a low order byte. The high order byte is often 
referi-ed to as the page of memory. For example, the address SI 637 
is in page S1 6 (22) , and S0277 is in page S02 (2). There is, however, 
a special mode of addressing known as "zero page" addressing 
and it is, as the name implies, associated with the addressing of 
memory locations in page zero. These addresses have a high order 
byte of zero. The zero page mode of addressing only expects one 
byte to describe the address, rather than two when using an 
absolute address, which saves speed and time. This mode tells the 
microprocessor to assume that the high order address is zero. 
Therefore zero page addressing can reference memor/ focations 
whose addresses are between SOOOO, and SOOFF. 

THE STACK 

The 6502 microprocessor (like almost all others) has what is 
known as a "stack/' This is used both by the programmer and the 
microprocessor to temporarily remember things, and to remember 
the order of events. The GOSUB statement in BASIC, which allows 
the programmer to call a "subroutine," must remember where it is 
being called from. When the RETURN statement is executed in the 
subroutine, the BASIC interpreter "knows" where to go back in 
order to continue executing. When a GOSUB statement is 
encountered In a program by the BASIC interpreter, the BASIC 
interpreter "pushes" its current position onto the stack before going 
to do the subroutine, and when a RETURN is executed, the 
interpreter "puils ' from the stack the information that tells it where it 
was before the subroutine call was made, so that it may continue as 
if nothing had happened. The interpreter uses instructions like PH A 
which will push the contents of the accumulator onto the stack, and 
PLA (the inverse) which will pulf a value off the stack into the 
accumulator. The status register can also be pushed and pulled 
with the PHP, and PLP respectively. 

The stack is 256 bytes long, and is located in page one of 
memory. It is therefore from 301 00 to $01 FF. It is organized 

133 



backwards in memory, i.e., the first position in the stack is at S01 FF, 
and the last is at SOI 00. Another register in the 6502 microproces- 
sor that hasn't been mentioned yet is called the "stack pointer," and 
it always points at the next available location in the stack. When 
something is pushed onto the slack, it is placed where the stack 
pointer points to, and the stack pointer is moved down to the next 
position (decremented). When something is pulled off the stack, the 
stack pointer is incremented, and the byte pointed to by the stack 
pointer (at SOI 00 offset by the contents of the stack pointer) is 
placed into the specified register. 

Up to this point, we have covered immediate, zero page, and 
absolute mode instructions. We have also covered (but have not 
stated) the 'implied " mode, which means that the instruction itself 
tells what registers/flags/memory the instruction is referring to. The 
examples we have seen are PHA, PLA, PHP, and PLP, which refer 
to stack processing and the accumulator and status registers. 

The X register will be referred to as X from now on, and similarly 
with A - accumulator, Y - Y index register, S - stack pointer, and 
P - processor status). 

INDEXING 

Indexing pfays an extremefy important part in the running of the 
6502 microprocessor. It can be defined as ''creating an actual 
address from a base address plus the contents of either the X or Y 
index registers." 

For example, if X contains S05, and the microprocessor executes 
an LDA instruction in the ^'absolute X indexed mode" with base 
address, e.g., S9000, then the actual location that is loaded into the 
A register is S9000 + 805 ^ S9005. The mnemonic format of an 
absolute indexed instruction is the same as an absolute instruction 
except a "\X" or ",Y" denoting the index is added to the address, 
e.g.: 

LDA $9000,X 

INDIRECT INDEXED ADDRESSING 

This mode altows the program to choose a memory location from 
256 adjacent locations. The address of the lowest location is stored 
in zero page, and the value in the Y register is added to that address 
to choose the final address. 

For example, we will place a S45 in location $01, and a $1 E in 
location $02. We will use the instruction to load the accumulator in 
the indirect indexed mode, specifying zero page address SOI as the 



134 



L 

r 



L 



location where the address to be used is held. Then the actual 
address will be comprised of: 

low address byte ^ contents of S01 ^ $45 
high address byte ^ contents of S02 = $1E 
Y register = S10 

The actual address = S1E45 + Y = $1E55 

If you think of indexed addressing like delivering junk mail 
through a post office, here is the principle for indirect Indexed 
addressing: 

We will deliver the letters to alf the houses on the block starting at 
S1E00 fvlemory St. and continuing for 256 houses. Here is the 
equivalent program for VICMon: 



A 1200 LDA #S0O 
A 1202 STAS01 

A 1204 STASFE 
A 1206 LDA #S1E 
A 1208 STA $02 

A 120ALDA#$96 
A 120C STA SFF 
A 120E LDY #S00 
A 1210 LDA #S66 

A 1212 STA (SOI).Y 

A 1214 LDA #SOA 
A 1216 STA (SFE),Y 

A 1218 INY 

A 1219 BNE S1210 

A 121 B BRK 

G 1200 



load low order actual base address 

set the low byte of the first indirect 

address 

set the low byte of the second address 

foad high order indirect address 

set the high byte of the first Indirect 

address 

load the second address's high byte 

set the high byte of the second address 

set the Indirect index (Y) 

66 is the value of our "letter" to the first 

"block^' 

store the "fetter" in the house on the 

first "block'' 

OA is the value of our second "letter" 

store the "letter" in the house on the 

second "block" 

add 1 to index 

branch back & send next letter 

return to VICfvlon when done 

sends the ^letter" — fills the top of the 

screen with blue & red lines! 



INDEXED INDIRECT ADDRESSING 

This mode allows the program to choose an address from a tabfe 
in page zero. Since page zero space is limited to 256 bytes, this is a 
mode that isn't used too often. 

This mode only works with the X register. It is like indirect 
Indexed, except that the zero page location is indexed, rather than 
an address stored in zero page. Therefore, the address stored in 



135 



page zero is the actual address because the index has already 
been used in the indirection. 

Let us fill location $05 with $45, and location S06 with 31 E. If the 
instruction to load the accumulator in the indexed indirect mode is 
executed and the specified zero page address is SOI, then the 
actual address will be comprised of: 

low order = contents of (S01 +X) 
high order = contents of (S02 + X) 
X register = $04 

Thus the actual address will be in = S01 + X = $05 
Therefore, the actual address will be the indirect address 

contained in S05 and $06 which is $1E45 
This is like sending a mailing to a specific list of addresses. We 

will store a list in zero page, and send the "letter ' only to those in the 

list. Suppose the list of addresses starts at $00. Here ts a program 

to send a 'letter'^ to one of the addresses: 

LDA #$00 — load low order actual base address 

STA $06 — set the low byte of the indirect address 

LDA #$16 — load high order indirect address 

STA $07 — set the high byte of the indirect address 

LDX #$06 —set the indirect index (X) 

LDA ($00,X) —load indirectly indexed by X. 

BRANCHES AND TESTING 

Another very important principle in machine language is the 
ability to test, and detect certain conditions, in a similar fashion to 
the "IF. . THEN" structure in VIC BASIC. 

The various "flags" In the status register are affected by different 
instructions in different ways. For example, there is a flag that is set 
when an instruction has caused a zero result, and is reset when a 
result is not zero, 

LDA #$00 

This instruction will cause "the zero result flag" to be set, 
because the instruction has resulted in the accumulator containing 
a zero. 

There is a set of instructions that will, given a particular condition, 
"branch" to another part of the program. An example of a branch 
instruction is 'BEQ", which means "branch if result equal to zero." 
The branch instructions will "branch" it the condition is true, and if 
not, the program will continue onto the next instruction, as if nothing 
had occurred. The branch instructions branch not by the result of 



136 



L 



L 



the previous instruction(s), but by internally examining the status 
register. 

As was just mentioned, there is a "zero result" flag in the status 
register The "BEQ" instruction branches if the "zero result" Hag 
(known as "Z") is set. Every branch instruction has an opposite 
branch instruction. The BEQ instruction has an opposite instruction 
"BNE" ("branch on result NOT equal to zero, " i-e., "2" not set). 

The index registers have a number of associated instructions 
which modify their contents. For example, the "INX ' instruction will 
"increment the X index register." If the X register contained $FF 
before it was incremented (the maximum numbertheX register can 
contain), it will "wrap around" back to zero. If we wanted a program 
to continue to do something until we had performed the increment 
of the X index that pushed it around to zero, we could use the BNE 
instruction to continue "looping" around, until X became zero. 

Apart from INX, there is "DEX'\ which will decrement the X index 
register, li it is zero, it will wrap around to SFF. Similarly, there are 
"INY" and "DEY" for the Y index register. 

But what if a program didn^t want to wait until X or Y had reached 
(or not reached) zero? Well there are comparison instructions, 
"CPX" and "CPY", which allow the machine language programmer 
to test the index registers with specified values, or even the 
contents of memory locations. If we wanted to see if the X register 
contained $40, we would use the instruction: 

CPX #$40 compare X with the "value" S40. 

BEQ (some other branch to somewhere else m the program, if 

part of the this condition is "true." 

program) 

The compare and branch instructions play a major pan in any 
machine language program. 

The operand specified in a branch instruction when using 
VICMon is the address of the pari of the program the branch should 
goto, if taken. However, the operand is only, in fact, an "offset" from 
where the program currently is. to the address specified. This offset 
is just one byte, and therefore the range that a branch instruction 
can branch to is limited from 128 bytes backward, to 127 bytes 
forward; this is a total range of 255 bytes, which is, of course, the 
maximum range of values one byte can contain. VICMon will tell 
you if you branch out of range, by refusing to "assemble" that 
instruction. It is unlikely that you wiil be doing such huge branches 
for quite a while anyway. For nearly all situations this is adequate 
anyway. The branch is a 'quick" instruction by machine language 
standards because of this "offset" pnnciple as opposed to an 

137 



absolute address. VICMon allows you to type in an absolute 
address, and it calculates the correct offset. This is just one of the 
"comlorts" of using an assembler. 



Subroutines 

In machine language (in the same way as using BASIC) , you can 
call subroutines. The instruction to call a subroutine is 'JSR^' Gump 
to subroutine), followed by the specified absolute address. 

Incorporated in the operating system is a machine language 
subroutine that will PRINT a character to the screen. The CBM 
ASCII code of the character should be in the accumulator before 
calling the subroutine. The address of this subroutine is SFFD2. 

Therefore, to print *'HI" to the screen, the following program 
should be entered: 

A 1400 LDA #S48 load the CBM ASCII code of "H" 



A 1402 JSR SFFD2 
A 1405 LDA #S49 
A 1407 JSR SFFD2 
A 140A LDA #S0D 
A 140C JSRSFFD2 
A 140F BRK 
G 1400 



print it 

load the CBM ASCII code of 'T' 

print that too 

print a carriage return as well 

return to VICMon. 

will print "Hr' and return to VICMon 



The "PRI NT a character" routine we have just used is part of the 
KERNAL "jump table." The instruction similar to GOTO in BASIC, 
is "JMPr which means "jump to the specified absolute address." 
The KERNAL is a long list of "standardized" subroutines that 
control ALL input and output of the VIC 20, Each entry In the 
KERNAL JMP's to a subroutine in the operating system. This ^'jump 
table" resides at SFF84 to $FFF5 in the operating system, A full 
explanation of the KERNAL is in the ^^KERNAL REFERENCE 
SECTION ■ in this manual, but certain routines will be used here to 
show how easy, and effective, the KERNAL is. 

We will now use these new principles in another program which 
will help you to put these instructions into context: 

This program will display the alphabet using a KERNAL routine. 

The only new instruction introduced here is TXA "transfer the 
contents of the X index register, into the accumulator.'* 



A 1400 LDX #S41 

A 1402 TXA 

A 1403 JSR SFFD2 

A 1406 INX 

A 1407 CPX #S51 



X = CBM ASCII of "A\ 

A = X. 

print character 

bump count. 

have we gone past 'Z " 



L 



[ 



138 



• A 1409 BNE S1402 nc^-go back and do more. 

• A 140B BRK yes— return to VICMon. 

To see the VIC print the alphabet, type the familiar command: 

.G 1400 

The comments that are beside the program explain the program 
flow, and logic. If you are writing a program, write it on paper first, 
and test it in small parts if possible. 



139 



MCS6501-MCS6505 MICROPROCESSOR 


ADC 


Add Memory to Accumulator with Carry 


AND 


"AND" Memory with Accumulator 


ASL 


Shift Left One Bit {Memory or Accumulator) 


BCC 


Branch on Carry Clear 


BCS 


Branch on Carry Set 


BEQ 


Branch on Result Zero 


BIT 


Test Bits in Memory with Accumulator 


BMI 


Branch on Result Minus 


BNE 


Branch on Result not Zero 


BPL 


Branch on Result Plus 


BRK 


Force Break 


BVC 


Branch on Overflow Clear 


BVS 


Branch on Overflow Set 


CLC 


Clear Carry Ffag 


CLD 


Clear Decimal Mode 


CLI 


Clear Interrupt Disable Bit 


CLV 


Clear Overflow Flag 


CMP 


Compare Memory and Accumulator 


CPX 


Compare Memory and Index X 


CPY 


Compare Memory and Index Y 


DEC 


Decrement Memory by One 


DEX 


Decrement Index X by One 


DEY 


Decrement Index Y by One 


EOR 


"Exclusive-Or" Memory with Accumulator 


INC 


Increment Memory by One 


INX 


Increment Index X by One 


INY 


Increment Index Y by One 


JMP 


Jump to New Location 



L 



L 



L 



L 



140 



[ 



INSTRUCTION SET - ALPHABETIC SEQUENCE 


JSR 


Jump to New Location Saving Return Address 


LDA 


Load Accumulator with Memory 


LDX 


Load Index X with Memory 


LDY 


Load index Ywith Memory 


LSR 


Shift Right One Bit (Memory or Accumulator) 


NOP 


No Operation 


ORA 


"OR" Memory with Accumulator 


PHA 


Push Accumulator on Stack 


PHP 


Push Processor Status on Stack 


PLA 


Pull Accumulator from Stack 


PLP 


Pull Processor Status from Stack 


ROL 


Rotate One Bit Left (Memory or Accumulator) 


ROR 


Rotate One Bit Right (Memory or Accumulator) 


RTI 


Return from Interrupt 


RTS 


Return from Subroutine 


SBC 


Subtract Memory from Accumulator with Borrow 


SEC 


Set Carry Flag 


SED 


Set Decimal Mode 


SEI 


Set Interrupt Disable Status 


STA 


Store Accumulator in Memory 


STX 


Store Index X in Memory 


STY 


Store Index Y in Memory 


TAX 


Transfer Accumulator to Index X 


TAY 


Transfer Accumulator to Index Y 


TSX 


Transfer Stack Pointer to Index X 


TXA 


Transfer Index X to Accumulator 


TXS 


Transfer Index X to Stack Pointer 


TYA 


Transfer Index Y to Accumulator 



141 



The following notation applies to this summary: 



A 


Accumulator 


X. Y 


Index Registers 


M 


Memory 


P 


Processor StiatUi; Register 


S 


Stack Pointer 


• 


Change 




No Change 


+ 


Add 


A 


Logical AND 


- 


Subtract 


^ 


Logical Exclusive Or 


f 


Transfer from Stack 


4 


Transfer to Stack 


-t- 


Transfer to 


-t- 


Transfer from 


V 


Logical OR 


PC 


Program Counter 


PGH 


Program Countt^r High 


PGL 


Program Counter Low 


OPER 


OPERAND 


§ 


IMMEDIATE ADDRESSING HODF. 



I 

r 
I 
I 



[: 

[ 



Note: At the top of each table is located in parentheses a 
reference number (Ref: XX) which directs the user to 
that Section in the MCS6500 Microcomputer Family 
Ptogramaning Manual in which the instruction is defined 
and discussed * 



r 



142 



L 



^^^ Add memory to accumulator with carry ADC 

Operation: A + M + C ^ A, C M ^ C I D V 

CRef: 2.2.1) ///,-/ 



[ 



A^idressing 


Assembly Language 


OP 


S^o. 


No. 


Mode 


Form 


CODE 


Bytes 


Cycles j 


r mined i ate 


ADC ?:' Oper 


69 


2 


2 


Zoro Page 


ADC Oper 


65 


2 


3 


Zoro Page, X 


ADC Oper, X 


75 


2 


h 


Absoluce 


ADC Oper 


6D 


3 


k 


Absolute, X 


ADC Oper, X 


7D 


3 


^* 


Absolute » Y 


ADC Oper, Y 


79 


3 


/+* 


(Indirect, X) 


ADC (Oper, X) 


61 


2 


6 


(Indirect), Y 


ADC (Oper), Y 


71 


2 


5* 



* Add ] if page boundary is crossed. 



** " " 'VI i\'D ' ' m enia rv with accurnulalor AH II 

Logical AND to the accumulator 

Operation: aAM-»A NZCIDV 

(Ref: 2,2.3.0) ^ / 



Addriissing 


Assembly Language 


OP 


No, 


No. 


Mode 


Forra 


CODE 


Bytes 


Cycles 


Itmnediatc 


AND ? Oper 


29 


2 


2 


Zero Page 


AND Oper 


25 


2 


3 


Zero Page, X 


AND Oper, X 


35 


2 


4 


Absolute 


AND Oper 


2D 


3 


4 


Ab-solute, X 


AND Oper, X 


3D 


3 


4* 


Absolute, Y 


A.ND Oper, Y 


39 


3 


4* 


(Indirect, X) 


AND (Oper, X) 


21 


2 


6 


(Indirect), ¥ 


AND (Oper), Y 


31 j 


2 


5 



* Add 1 if page boundary Is crossed, 

143 



ASL ^^*- ^^'"^' ^ '"/^ ''^"^' f^ii( Memory or Accamuiator} 

Operatiuii; C ' 7 6 ^j ^ i 2 1 ^ -0 



ASL 



N * C I D V 

y / / 



(Riif: L0.2J 



Addressing 


Assembly Language 


OF 


No. 


No. 


Mode 


Form 


CODE 


Bytes 


Cycles 


AccuniiilJtor 


ASL A 


i*A 


1 


2 


Zero Page 


ASL Oper 


6)6 


2 


5 


Zero P-ige, X 


ASL Oper» X 


16 


2 


6 


Absolute 


ASL Opcr 


^E 


3 


6 


Absoitite, X 


ASL Oper. X 


IE 


l 


7 



BCC 



BCC Branch (m Carry Ck'ar 



BCC 



Oper.it ion: ar.:inch on C - 



N 5 C I D V 



(Ret: 4,1,1,3) 



Addres.sing 
Mode 


A^ s c ml J 1 y L .m ^ u ag c 

Form 


OP 
CODE 


No. 
Byteei 


Ho. 
Cycles 


Relative 


liCC Opcr 


n 


•1 


2* 



* Add 1 If branch occurs to same page. 

* Add 2 if branch occurs Co different page. 



BCS litrS Branch on catty set 

Ope rati on: Brancb on C » 1 

(Ref: ^A.IA) 



N a C I D V 



BCS 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


Relative 


BCS Oper 


B3 


2 


2* 



* Add I if branch occurs to same page, 

* Add 2 if branch occurs to next page. 

144 



r 

L 



BEQ 



BEQ Branch tm n'suft zero 
OpcratiOfi: Branch on S'^l NSCIDV 
(Ref: 4.1.1.5) 



BEQ 



Acidressing 
Mode 


Assembly Language 
Torn 


OP 
CODE 


No. 
Sytes 


No. 
Cycles 


Rclattve 


BEQ Opcr 


F0 


2 


2* 



* Add I if branch occurs to same page. 

* Add 2 if branch occurs to next page* 



PII bit Test bits in memory with accumuialor 

Operatlnn: A A M, M^ - N , M^ -^ V 

Bit 6 and 7 are transferred to the status register. S 2 C I D V 
If the result of AAM is zero then Z: ^ 1 » otherwise M-/ M^ 

(Ref: 4.2.1.1) 



BIT 



Z - 



Adelrtjssing 

Modt' 


Assembly Language 
Forn 


OP 
CODE 


So. 
Bytes 


^io. 

Cycles 


Zi^ro Page 
Abiitilute 


BIT Oper 
BIT Opcr 


24 
2C 


2 
3 


3 
4 



BMl 



BMI Branch on rem ft minus 
Operation: Branch on N =^ 1 

(Ref: 4.1.1.1) 



N * C I D V 



BMl 



Acidressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


So. 
Bytes 


So. 
Cycles 


Relative 


BMl Oper 


30 


2 


2* 



* Add 1 If branch occurs to sam^ page. 

* Add 2 if branch occurs to different page. 

145 



Dilt BNE Hranch tm rvsttU not zero 

Operation: HrJnch on Z - 

(Ref: ^. 1,1.6) 



BKE 



L 



K ^ C 1 D V 



Address! [iK 
Mode 


Assembly Language 
Kona 


OP 
CODE 


Bytes 


No. 
Cycles 


Relativi- 


bN]£ Oper 


m 


2 


2* 



* Add 1 if branch occi^rs to same page. 

* Add 2 if hr4:inch occurs to different page. 



BPL 



BPL li ranch art resuii pius 
Op^fration: Branch on N ^ 

(Ref; i.l.L.2) 



BPL 



N i* C I D V 



Addressing 
Mode 


Assembly Language 
Forn 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


Relative 


BPL Uper 


10 


2 


2* 



* Add 1 if branch occurs to same page. 

* Add 2 If branch occurs to different page. 



BRK 



BRK Force Brmk 



OpetiiLion: Forced Interrupt PC + 2 + p + 

(Ref; 9,11) 



K Z C 1 D V 



BRK 



Addressing 
Hodc^ 


Assembly Language 
Forra 


OF 

coDi: 


No. 
Bytes 


CyrU^. 


Implied 


BRK 


m 


1 


7 



1. A BRX copEand cannot be masked by setting I. 



146 



WWVi BVC Branch ffTi ijrct/Iow clear BVC 

Operation: Branch onV-0 N^ClDV 

(Ref: ^.1.1.8) 



Addressing 
Mode 


Assi;mbly Language 
Fora 


OF 
CODE 


No. 
Bytes 


No. 
Cycles 


Relative 


BVC Oper 


5(S 


2 


2* 



* Add 1 if branch occurs to saae page, 

* Add 2 if branch occurs to different page. 



BV5 ^yS Branch on overfluw set 

Ope rat ton: Branch on V - I N H C I D V 

(Ref: 4.1.1.7) 



BVS 



Add re !^ sing 
Mode 


Asstirtibly Language 
Form 


OP 
CODE 


Bytes 


Wo. 
Cycles 


Kiilac ive 


tiVS Oper 


7(3 


2 


2^ 



* Add 1 if branch occurs to saiae page. 

* Add 2 if branch occurs to different p^age. 



CIC 



Operation: * C 



CLC Clear carry JJag 



{iWi: 3.0.2) 



CLC 



N H C 1 D V 
_ ^ (3 . 



Addre.'ising 
Mode 


As s cmb 1 y Lan gu ag e 

Forn 


OP 
CODE 


No. 
Bytts 


Mo. 
Cycles 


Impl ied 


CLC 


18 


1 


Z 



147 



CLD 

Operation: -i- D 



CLD Clear decimal made 



(Ref: 3.3.2) 



CLD 



S £ C I D V 



Addressing 
Mode 


Assembly Language 
Forro 


OP 

CODE 


No. 
Bytes 


Cycles 


ImpUed 


CLD 


D8 


1 


2 



[ 

L 



VLI CLI Clear mtermpt disable bit 

Operation ; -^ I 

(Ref: 3.2.2) 



CLI 



N 2 C I D V 

- - 



Addressing 
Mode 


Assembly Language 
Furra 


OP 
CODE 


Bytes 


No. 
Cycles 


Implied 


CLI 


58 


1 


2 



[ 



L 

r 



CLV 



Operation! : ^ V 



Addressing 
Mode 



Implied 



CLV Clear over/low /Jag 



(Ref: 3.6,1) 



Assembly Language 

Form 



CLV 



N £ C I D V 



OP 

CODE 



B8 



Ho. 
Bytes 



CLV 



No. 

Cycles 



L 



143 



r 



LMr CMP Compare memory and accumutator 

Operation: A-H NSCIDV 

CRL^ft 4,2.1) 



CMP 



/ / / — 



AddriissinR 


Assembly Languaj^e 


OP 


No. 


No. 


Mode ' 


Form 


CODE 


Bytes 


Cycles 


IraciediaLe 


CHP #Opet 


C9 




2 


Zero Page 


CMP Oper 


C5 




I 


Zero Page, X 


CKI^ Oper. K 


Di 




i* 


Absolute 


C-KI* Oper 


CD 




h 


Absolute, X 


CMP Oper. X 


DD 




4* 


Absolute, Y 


CTIP Oper» Y 


D9 




4* 


flndirect, X) 


CHP (Oper, X) 


CI 




6 


(Indirect), Y 


CMP (Oper), ¥ 


Dl 




5* 



• Add 1 if page boundary is crossed. 

vi A CPX Compare Memory and Index X 

Operation; X - M 

(Ret: 7.8) 



CPX 



N a c I D V 
/ / / 



Addressing 
Mode 


Assembly Language 
Foni 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


Immediate 
Zczo Page 
Absolute 


CPX #Oper 
CPX Oper 
CPX Oper 


E4 
EC 


2 
2 
3 


2 
3 

4 



CPY 

Operation: Y 



CPY Com part' memory and ittdi^x Y 



(Ref: 7.9) 



CPY 



N S C I D V 
/ / / ^ - - 



Addressing 
Mode 


Assembly Language 
For^q 


OP 
CODE 


No. 
Bytes 


Ho. 
Cycles 


IinmedLate 
Zero Page 
Absolutt; 


CPY* Oper 
CPY Oper 
CPY Oper 


C0 
C4 
CC 


2 
2 
3 


2 

3 
4 



149 



DEC 



DEC Dciircment memory by one 
Operation: M - J * M 

(Ref: 10.7) 



DEC 



N a C I D V 



Addressing 
Mode 


AsscTnbly Language 
Form 


OP 
CODf- 


No, 
Bytes 


No. 
Cycles 


Zero Page 
Zero Page, X 
Absolute 
Absolute, X 


DEC Oper 
IJKC Oper, X 
DKC Dper 
DEC Oper, X 


C6 
D6 

ct: 


2 
2 

3 


5 
6 
6 

7 



DEX 



DEX Decn*fnenr itttiex X hy one 
Operation: X - 1 ^^ X 

(Hef: 7,6) 



DEX 



N a C I D V 
y / 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


Implied 


DEX 


CA 


: 


2 



L 



L 



"*' DEY ikcn-menrindex Y by one DIY 

Operation: Y - 1 ^ Y N ii C I D V 

/ / 

(Kef: 7.7) 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


Inapl led 


DEY 


88 


1 


2 



150 



r 



CU R EOR ' ^Exchaive- Or ' * memory with acatm uhtor 

Opcrattoni A¥M^A NSCIDV 

(Ref: 2.2.3.2) "^ / ---- 



EOR 



Addressing 
Mode 


AsseKibly Language 
Form 


OP 
CODE 


No. 
Bytes 


Mo. 
Cycles 


Immediate 


EOR #Oper 


49 


2 


l 


Zero Page 


EOR Opcr 


A5 ! 


2 


1 


Zero Page, X 


EOR Oper, X 


55 


2 


4 


Absolute 


EOR Oper 


^D 


3 


A 


Absolute, X 


EOR Oper, X 


5D 


1 


i,± 


Absolute, Y 


EOR Oper, V 


59 


3 


4* 


(Indirect, X) 


EOR (Oper, X) 


hi 


2 


6 


(Indirect), Y 


EOR (Oper), Y 


51 


2 


5* 



* Add 1 If page boundary is crossed. 



in^ INC Increment memory by Qt\c 

Opt^ratlon: M + 1 -^ M 

(Ref; 10.6) 



INC 



^f a c I D V 



Addressing 


Assembly Language 


OP 


So. 


No. 


Mode 


Fora 


CODE 


Bytes 


Cycles 


Kero Page 


INC Oper 


E6 


2 


5 


Zero P.ige, X 


INC Oper. X 


F6 


2 


6 


Absolute 


INC Oper 


EE 


3 


6 


Absolute, X 


INC Oper, X 


FE 


3 


7 



I HA JNX Increment Ifidcx X by one 

Operation: X + 1 -^ X 

(Ref: 7.-i) 



INX 



N a C 1 D V 
/ / 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No. 

Bytes 


No. 
Cycles 


Implied 


INX 


E8 


1 


2 



161 



IHY TNY fnvn^ment hidcx Y by one 

Operation J Y + 1 ^ V 

(Ref: 7.S) 



INY 



S H C I D V 
/ / 



Addressing 
Mode 


Assembly Language 

Form ' 


OP 
CODE 


No, 
Bytes 


Cyc Lc3 


Implied 


INY 


ce 


I 


2 



JMP JMP Jump to new lot a t to f^ 

Operation: (PC + U -^ PCL 



JMP 



N g C I D V 



(PC -^ 2) ^ FCH 



(Ref: i.0,2) 
(Ref: 9.8,1) 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No. 
By cei* 


Cycles 


Absolute 
Indirect 


JMP Oper 
JMP COper) 


4C 
6C 


3 
3 


3 
5 



J5R JSR Jif^^ip to nt:w hcalion scving return address 

Operation: PC + 2 ^ , (PC + 1) ^^ PCL N g C I D V 



JSR 



(PC + 2) ^ PCH 

(Ref: B,l) 



Addressing 
Mode 


Assembly Language 
Fona 


OF 
CODE 


No. 
Bytes 


No. 
Cycles 


Absolute 


JSR Oper 


20 


3 


& 



152 



LDA 



LDA Load accumulator with memory 



LDA 



Oper^cion: Jl ^ A 



(Ref: 2.1.1) 



N E C I D V 
/ / 



Addrt^s in^^ 


AEsetr.bly LangUtige 


OP 


No, 


No, 


Hade 


Tarn 


CODE 


Bytes 


Cycles ' 


T mmt d i a t L' 


LDA^Oper 


A9 


2 


2 


Zero Vagv 


LDA Oper 


AS 


2 


3 


Zero Page, X 


LDA Oper, X 


B!3 


2 


4 


Absolute 


LDA Oper 


AD 


J 


4 


Absolute, >: 


LDA Oper, X 


BD 


3 


4* ' 


Absolute, Y 


LDA Oper, Y 


B9 


3 


U* 


C Indirect X) 


LDA {Oper, X) 


Al 


2 


b 


(Indirect) , V 


LDA (OperJ, Y 


El 


2 


3* 



* Add 1 if pagL' boundary is crossed. 



LUA LDX Load index X i^ith menmry 

Operations M -• X 

(Ref: 7.0) 



N Z C I D V 

/ V 



LDX 



Addressing 
Mode 


Assembly Language 
Forna 


OP 
CODE 


No, 
Bytes 


No. 
Cycles 


Iranediiite 


LDX f^ Oper 


A2 


2 


2 


Ztro i\'ige 


LDX Oper 


A6 


2 


3 


Zero Page, Y 


LDX Oper, Y 


B<3 


2 


4 


Absolute 


LDX Oper 


AE 


3 


4 


Absolute. Y 


LDX Oper, Y 


BE 


3 


4* 



* Add 1 when p^ge boundary is crossed, 

153 



LDY 

Operation: M -^ Y 



LDY L<}ad index Y with memory 



(Ref: 7.1) 



LDY 



N S C r D V 

/ / 



Addressing 


Assembly Language 


OE* 


No, 


No. 


Mode 


Form 


CODE 


Bytes 


Cycles 


ImiDedlate 


LDY *Oper 


A0 


2 


2 


Zero Page 


LDY Oper 


A4 


2 


3 


Zero Page, X 


LDY Opcr, X 


B4 


2 


4 


Absolute 


LDY Oper 


AC 


3 


4 


Absolute, X 


LDY Oper, X 


BC 


3 


A* 



Add 1 when page boundary is crossed. 



L)K LSR Shift rif^ht tme bit (memon' or accumutatnt} 



I 



ISR 



7 


6 


^ 
^ 


4 


i 


2 


1 






-* c 



(Ref: 10.1) 



N K C I D V 

/ / - -^ 



Addressing 

Mode 


A-'j5>enibly Language 

Form 


OP 
CODE 


No. 
Bytes 


Ho. 
Cycles 












Ac JuinulaCor 


LSR A 


4A 


I 


2 


Zero Page 


LSR Oper 


U6 


2 


5 


Zero Page, X 


LSR Oper, X 


56 


2 


6 


Absolute 


l,SR Opt^r 


^K 


3 


6 


Absolute, X 


rSR Oper, X 


5K 


3 


7 



NOP NOP No operation 

Operation: No Operation (2 cycles) 



N a C I D V 



NOP 



Addressing 
Mode 


Assembly Language 

Form 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


TiDplled 


NOP 1 


EA 


1 


2 



r 



154 



UK A ORA "OR " metTujry with accumuialur OR A 

Operation: AVM^A NECIDV 

(Ref: 2.2.3.1) "^ '^ 



Addressing 
xMode 


A?^sembly Language 

torn 


OP 
CODE 


No. 
Bytes 


Cycles 


InimediatL' 


ORA ffOpet 


09 


2 


2 


Zero Page 


ORA Oper 


05 


7 


3 


Zero Pagf, X 


ORA Opet, X 


13 


2 


^H 


Absolute 


ORi\ Oper 


0D 


3 


4 


Absolute, >: 


ORA Oper» X 


ID 


3 


A* 


Absolute, ¥ 


ORA OpiiT, V 


19 


1 


A* 


(Indirect, X) 


ORA (Opcr, X) 


01 


■T 


6 


(Indirect). Y 


ORA (OpL-r) , Y 


11 


2 


5 



* Add 1 on page crossing 



PHA 


PH A Piixh iiiTu ft fit III u tr o fi .stack 




PHA 


Operation: A * 


N K C 1 D V 








(Rcf: &.b) ^^-^^_ 






Addressing 


Assembly Language 


OP 


N'i) . 


t;o. 


i 


Mode 


Form 


CODE 


aytes 


Cycles 1 




Implied 


PHA 


4B 


1 


3 



PHP 

Operation: FJ- 



PHP Pusft processor status on stack 



(Ref: a.U) 



PHP 



N a C 1 D V 



Addressing 

>!ode 


Assembly Language 
Tor a 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


Implied 


PHP 


08 


I 


3 



155 



PU 



Operation: A 



P LA Full a ecu rn u la to r from s tack 



(Rcf: e.6) 



PLA 



N a C 1 D V 

/ / -■ 



Addressing ' 
Mudc 


A^Henibly Language 
Form 


OF 
CUDK 


No, 
Bytes 


No. 
Cycl*^£i 


Imp lied 


PLA 


6B 


1 


A 



L 

r 



[ 



r Ir PLP Pull processor status from stack 

Operation: P t 

(Riif: S.13) 



N S C I D V 
FrDfli Stack 



PLP 



Addressing 
H0dc 


Assembly Language 
Forr. 


OP 
CODF. 


So. 
Bytes 


Ho. , 
Cycles i 


Implied 


PLP 


1 28 


1 


t* 



ROL 



ROL Rotate one hit left {memory or accumidaior) 



C M or A ' ] 

|7j6j5j4|3|2|l|a] - ZE7 ^1 



(Ref: 10,3) 



N a C 1 D V 
/ / / ' 



ROL 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No. 
Bytes 


Ho. 
Cycles 


Accumulator 


ROL A , 


2A 


1 


2 


Zero Page 


ROL Oper 


26 


2 


5 


Zero Page, X 


ROL Oper, X 


36 


2 


6 


Absolute 


ROL Oper 


2E 


1 


6 


Absolute, X 


ROL Oper, X 


3E 1 


3 


7 



r 



156 



ROR 



RO R Rota te one bit ngh t {memory or accumulator} 



ROR 



Operation; 




2 1 



(Ref: 10. A) 



N S C T D V 

/ / / 



Addressing 
Mode 


Assembly Language 
Fonn 


OP 
CODE 


No, 
Bytes 


No. 
Cycles 


Accutnulacor 


ROR A 


6A 




2 


Zero Page 


ROR Oper 


66 




5 


Kero Page,X 


ROR Oper.X 


76 




6 


Absolute 


ROR Oper 


6E 




6 


Absolute, X 


ROR Oper.X 


7E 




7 



Note: ROR instruction will be available on HCS650X micro- 
processors after June* 1976. 



RTt 



Operation: F* PC+ 



RT[ Return from interrupt 



(Ref: 9.6) 



RTI 



N a C I D V 
From Stack 



Addressing 
^forie 


Assembly Language 
Fonn 


OP 
CODE 


So. 
Bytes 


No. 
Cycles 


Implied 


RTI ; 


^eJ 


1 


6 



RTS 



RTS R e turn from su brou tin e 
Operation: PCt, PC + 1^ PC 

{Ref: 8,2) 



RTS 



N a C 1 D V 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No, 
Bytes 


No. 
Cycles 


IiopUed 


RTS 


63 


1 


6 



157 



^BC SBC Sub i Fact memory from accumuhrnr with harrow 
Operation: A - M - C - A N 2 C 1 D V 
^:ote: C = Ecrrcv (Ref : 2.2,2) /// / 



SBC 



Addressing 
Mode 


Assembly Language 
Fonn 


OP 
CODE 


No. 
Bytes 


No. 
Cycles 


Imraediate 


SBC *Oper 


E9 


2 


2 


Zero Page 


SBC Oper 


£5 


2 


3 


Zero Page, X 


SBC Oper, K 


F5 


2 


4 


Absolute 


SBC Oper 


ED 


3 


A 


Absolute, X 


SBC Oper, X 


FD 


3 


A* 


Absolute, Y 


SBC Oper, Y 


F9 


3 


U^ 


(Indirect, X) 


SBC (Oper, X) 


El 


2 


6 


(Indirect), Y 


SEC (Opt^r), Y 


Fl 


2 


3* 



* Add 1 when page boundary is crossed. 



SEC 



Operation: 1 -*■ C 



SEC Set ttirn fhg 
(Ref: 3.0,1) 



SEC 



N a C 1 D V 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


No. 
Bytes 


Cycie,^ 


Implied 


Stic 


38 


1 


2 



r 



r 

L 



r 



SiD SED Set decimal mode 

Operation: 1 -^ D 

(Ref : 3,3,1) 



N a C I D V 



SED 



Addressing 
Mode 


Assenibly Language 
Form 


OP 

CODE 


So. 
Bytes 


No. 
Cycles 


Implied 


SED 


EE 


1 


2 



r 



158 



J * I SRI St ' I in t**tnip t disable Ua tus 

Opt; rat ion: 1 ■* I 

CKef: 3.2.1) 



SEI 



N a C I D V 



Add revising 
Mode 


Assembly Language 
Form 


OP 
CODE 


Bytes 


No. 
Cycles 


Implied 


SEI 


76 


1 


2 



^'^ ^ A Store accumuiator in memory' 5TA 

Operatlonj A^M NZCIDV 

(Refi 2.1.2) 



Addmssing 
Mode 


Assembly Language 
Form 


OP 
CODE 


So. 
Bytes 


No. 
Cycles 


Zero Page 


STA Opcr 


65 


2 


3 1 


Zero Page, X 


STA Opcr. X 


95 


2 


^ 


Absolute 


STA Oper 


8D 


3 


4 


Absolute , X 


STA Oper, X 


9D 


3 


5 


Absolute, Y 


STA Oper, Y 


99 


3 


5 


(Indirect, X) 


STA (Opcr, X) 


81 


2 1 


6 


Clndlrect), Y 


STA (Oper), Y 


91 


2 


6 



STX 

Operot ion : X +^ M 



STX Store imlex X in memory 



N a C 1 D V 



STX 





(Ref: 7.2) 








Addressing 
Hode 


Assembly L^anguage 
Form 


OP 
CODE 


Ko. 
Bytes 


No. 
Cycles 


Zero Page 
Zero Page, Y 
Absolute 


STX Opcr 
STX Oper, Y 
STX Oper 


86 
96 

8E 


2 

2 
3 


3 



159 



jl 1 STY Sf(jrt' index Y iri memory 

Operation: ¥ * H 

{Kef: 7.3) 



STY 



N a C I D V 



Addressing 
Mode 


Assembly Language 
Form 


OP 
CODE 


Ho. 
Bytes 


No. 
Cycles 


Zero Page 
Zero Page, X 
Absolute 


STY Oper 
SIT Opet, X 

STY Ope r 


8C 


2 ' 

2 

3 


3 
4 



I AX TAX Transfer accumulator to index X I AA 

Operation; A-^X N3C1DV 

/ / 

CRef: 7.11) 



Addressing 
Mode 


Assembly Language 


OP 

CODE 


No. 
Bytes 


No. 
Cycles 


Implied 


TAX 


AA 


1 


2 



TAY 



TAY Tnimfcr accumulator to indvx Y 

Operation: A - Y f^ a C I D V 

/ / - 

CRcf: 7.13) 



TAY 



Addressing 

Mode 


Asseuvbly Language 

For in 


OP 

CODE 


No. 
Bytes 


No. 

Cycles 


Implied 


TAY 


A8 


1 


2 



L 



L 
L 



[ 

r 



160 



1 5J[ TSX TransftT Mack pointer ro index X 

Opfratlon: S-X NSCIDV 



TSX 



Addressing 

Mode 


AtiiJumbly Language 
Form 


OP 
COTJK 


No. 
Bytes 


Ho. ■ 
Cycles 


Implied 


rsx 


BA 


1 


2 



TX A ^"XA Transjlr w Jc.r X nt accumulator TX A 

Operation; X ^ k N S C 1 D V 



(Ret r 7.12) 



//-___ 



Addressing 
Kode 


Assembly Language 
Forra 


CODE 


:;o* 

Bytes j 


Cycles 


l^^pJied 


TXA 


BA 


1 


2 



lA^ TXS Trutniji'r mdcx X fosimk poinier TXS 

operation: X - S N K C I D V 
(Kuf: S.8> 



Addressing, 
Mode 


Aiisembly Language 
Form 


DP 

coi;e 


No. 
Bytes 


Cycles 


Implied 


TXS 


9 A 


I 


2 



TlA TYA Transfer index Y to aratmulaUjr 

Operation: Y - A j; ^ c 1 B V 

(Ref: 7.U) 



TfK 



Addressing 

Mode 


As.^crbly Langua^i- 
For::! 


or 

CDDt: 


^'o. 

Bytes 


No. 
Cycles 


Implied 


TYA 


98 


1 


2 



161 



INSTRUCTION ADDRESSING MODES AND 



£ 



X > 



X > 

o ^ . 

ffl 2 g, S & ^ a; © 

3E2££lS8t3^|| 



-a 

V i^ Ti *^ 



r 



ADC 

AND 

ASL 

BCC 

BCS 

BEQ 

BIT 

BMI 

BNE 

BPL 

BRK 

BVC 

BVS 

CLC 

CLD 

CLI 

CLV 

CMP 

CPX 

CPY 

DEC 

DEX 

DEY 

EOR 

INC 

1NX 

tNY 

JMP 



2 3 4 
2 3 4 
5 6 



2 3 

2 3 

2 3 

. 5 



2 3 4 
. 5 6 



4 4* 4* 
4 4- 4* 
6 7 



4 

4 
4 
6 



4 4' 
6 7 



4* 4 



2 
2 
2 
2 



2 
2 



4* 



2 
2 



6 5* 

6 5* 



2" 

2*' 
2" 

2" 
2" 

2" 

2.. 



6 5 



6 5 



L 



L 



" Add one cycle if indexing across page boundary 

•• Add one cycle if branch is taken. Add one additional 



162 



r 



RELATED EXECUTION TIMES (in clock cycles) 



X > 



X > 



m ® O 

- IS » 

i 1 



p .2 ^ ? g* 5 £ £ « 

^ iwi iti iti #^ rf< ^ ^ (3^ 



< = N N N < < 






O 



u 

SI 

c 



u 
2 



JSR 

LOA 

LDX 

LOY 

LSR 

NOP 

ORA 

PHA 

PHP 

PLA 

PLP 

ROL 

ROR 

RTI 

RTS 

SBC 

SEC 

SEO 

SEI 

STA 

STX» 

STV 

TAX 

TAY 

TSX 

TXA 

TXS 

TYA 



2 
2 



2 
2 
2 



2 3 4 



3 
3 
3 

5 



5 
5 



4 
6 



6 
6 



2 3 4 



3 
3 
3 



6 
4 
4 
4 
6 



6 

6 



4 
4 
4 



4* 4" 
. 4* 
4* 
7 



4 4* 4 



4 4*4 



5 5 



3 
3 
4 
4 



6 
6 

2 
2 
2 



2 
2 
2 
2 
2 
2 



6 5* 



5* 



6 5 



6 6 



if branching operation crosses page boundary 



tea 



00 - BRK 

01 - ORA - (Indirect. X) 

02 - Future Expansion 

03 - Future Expansion 

04 - Future Expansion 

05 " ORA - Zero Page 

06 - ASL - Zero Page 

07 - Future Expansion 
0S - PHP 

09 - ORA - Immediate 
0A - ASL - Accumulator 
0B - Future Expansion 
0C - Future Expansion 
0D - ORA - Absolute 

0E - ASL - Absolute 
0F - Future Expansion 

10 -- BPL 

11 - ORA - (Indirect) ^Y 

12 - Future Expansion 

13 - Future Expansion 

14 - Future Expansion 

15 - ORA - Zero Page,X 

16 - ASL - Zero Page,X 

17 - Future Expansion 

18 - CiC 

19 - ORA - Absolutely 
lA - Future Expansion 
IB - Future Expansion 
IC - Future Expansion 
ID - ORA - Absolute, X 
IE - ASL - Absolute, X 
IF - Future Expansion 



20 - JSR 

21 - AND - ( Indirect, X) 

22 - Future Expansion 

23 - Future Expansion 

24 - BIT - Zero Page 

25 - AND - Zero Page 

26 - ROL - Zero Page 

27 - Future Expansion 

28 - PLP 

29 - AND - Immediate 
2A - ROL - Accumulator 
2B - Future Expansion 
2C - BIT - Absolute 

2D - AND - Absolute 
2E - ROL - Absolute 
2F - Future Expansion 

30 - BMl 

31 - AND - (Indirect), Y 

32 - Future Expansion 

33 - Future Expansion 

34 - Future Expansion 

35 - AND - Zero Page,X 

36 - ROL ' Zero Page,X 

37 - Future Expansion 

38 - SEC 

39 - AND - Absolute, Y 
3A - Future Expansion 
3B - Future Expansion 
3C - Future Expansion 
3D - AND - Absolute, X 
3E - ROL - Absolute, X 
3F - Future Expansion 



r 

L 



164 



L 



40 - RTI 

41 - EOR - (Indirect, X) 

42 - Future Expansion 

43 - Future Expansion 

44 - Future Expansion 

45 - EOR - Zero Page 

46 - LSR - Zero Page 

47 - Future Expansion 

48 - PHA 

49 - EOR " Idmediate 
4A - LSR - Accumulator 
4B - Future Expansion 
4C - JMP - Absolute 

4D - EOR - Absolute 
4E - LSR - Absolute 
4F - Future Expansion 

50 - BVC 

51 - EOR - (Indirect), Y 

52 ^ Future Expansion 

53 - Future Expansion 

54 - Future Expansion 

55 - EOR - Zero Page,X 

56 - LSR - Zero Page.X 

57 - Future Expansion 

58 - CLI 

59 - EOR - Absolutely 
5A - Future Expansion 
5B - Future Expansion 
5C - Future Expansion 
5D - EOR - Absolute, X 
5E - LSR - Absolute, X 
5F - Future Expansion 



60 - RTS 

61 - ADC - (Indirect, X) 

62 - Future Expansion 

63 - Fucure Expansion 

64 - Future Expansion 

65 - ADC - Zero Page 

66 - ROR - Zero Page 

67 - Future Expansion 
6S - PLA 

69 - ADC - Immediate 
6A - ROR - Accuraulator 
6B - Future Expansion 
6C - JMP - Indirect 

6D - ADC - Absolute 
6E - ROR - Absolute 
6F - Future Expansion 

70 - BVS 

71 - ADC - (Indirect) ,Y 

72 - Future Expansion 

73 - Future Expansion 

74 - Future Expansion 

75 - ADC - Zero Page.X 

76 - ROR - Zero Page,X 

77 - Future Expansion 

78 - SEI 

79 - ADC - Absolute, Y 
7A - Future Expansion 
7B - FuCure Expansion 
7C - Future Expansion 
?D - ADC - Absolut e,X 
7E - ROR - Absolute, X 
7F - Future Expansion 



165 



80 - Future Expansion 

81 - STA - (Indirect, X) 

82 - Future Expansion 

83 - Future Expansion 
S4 - STY - Zero Page 

85 - STA - Zero Page 

86 - STX - Zero Page 

87 - Future Expansion 

88 - DEY 

89 - Future Expansion 
8A - TXA 

8B - Future Expansion 

aC - STY - Absolute 

iJD - STA - Absolute 

8E - STX - Absolute 

87 - Future Expansion 

90 - BCC 

91 - STA - (Indirect},Y 

92 - Future Expansion 

93 - Future Expansion 
9A - STY - Zero Page.X 

95 - STA - Zero Page,X 

96 - STX - Zero Page,Y 

97 " Future Expansion 

98 - TYA 

99 - STA - Absolute, Y 
9A - TXS 

9B - Future Expansion 

9C - Future Expanjjion 

9D - STA - Absolute, X 

9E - Future Expansion 

9F - Future Expansion 



A0 


- LDY - 


Immediate 


Al 


- LDA - 


(Indirect, X) 


A2 


- LDX - 


Iinmediate 


A3 


- Future Expansion 


A£* 


- LDY - 


Zero Page 


A5 


- LDA - 


Zero Page 


A6 


- LDX - 


Zero Page 


A7 


- Future Expansion 


A8 


- TAY 




A9 


- LDA - 


Immediate 


AA 


- TAX 




AB 


- Future Expansion 


AC 


- LDY - 


Absolute 


AD 


- LDA - 


Absolute 


AE 


- LDX - 


Absolute 


AF 


- Future Expansion 


B0 


- BCS 




Bl 


- LDA - 


(Indirect) ,Y 


B2 


- Future Expansion 


B3 


" Future Expansion 


B4 


- LDY - 


Zero Page,X 


B5 


- LDA - 


Zero Page,X 


B6 


- LDX - 


Zero Page.Y 


B7 


- Future Expansion 


B8 


- CLV 




B9 


- LDA - 


Absolute, Y 


BA 


- TSX 




BB 


- Future Expansion 


BC 


- LDY - 


Absolute, X 


BD 


- LDA - 


Absolute, X 


BE 


- LDX - 


Absolute, Y 


BF 


- Future 


I Expansion 



166 



r 



C0 - CPY - Iinmediate 
CI - CMP - (Indirect, X) 
C2 - Future Expanston 
C3 - Future Expansion 
G^ - CPY - Zero Page 
C5 - CMP - Zero Page 
C6 - DEC - Zero Page 
C7 - Future Expansion 
C8 - INY 

C9 - CMP - Itnmediate 
CA - DEX 

CB - Future Expansloo 
CC - CPY - Absolute 
CD - CMP - Absolute 
CE - DEC - Absolute 
CF - Future Expansion 
D0 - BKE 

Dl - CMP - (Indirect>,Y 
D2 ~ Future Expansion 
D3 - Future Expansion 
D^ - Future Expansion 
D5 - CMP - Zero Page,X 
06 - DEC - Zero Page.X 
D7 - Future Expansion 
D8 - CLD 

D9 - CMP - Absolutely 
DA - Future Expansion 
DB - Future Expansion 
DC - Future Expansion 
DO -CMP - Absolute,X 
DE - DEC - Absolute, X 
DF - Future Expansion 



E0 - CPX - Immediate 
El - SBC - (IndirectjX) 
E2 - Future Expansion 
E3 - Future Expansion 
E4 - CPX - Zero Page 
E5 - SBC - Zero Page 
E6 - INC - Zero Page 
E7 - Future Expansion 
E8 - INX 

E9 - SBC - Immediate 
EA - MOP 

EB - Future Expansion 
EC " CPX - Absolute 
ED - SBC - Absolute 
EE - IKC " Absolute 
EF - Future Expansion 
F0 - BEQ 

Fl - SBC - (Indirect) ,Y 
F2 - Future Expansion 
F3 - Future Expansion 
F4 - Future Expansion 
F5 - SBC - Zero Page.X 
F6 - IKC - Zero Page,X 
F7 - Future Expansion 
FS - SED 

F9 - SBC - Absolute, Y 
FA - Future Expansion 
FB - Future Expansion 
FC - Future Expansion 
FD - SBC - Absolute, X 
FE - INC - Absolute, X 
FF - Future Expansion 



167 



SPECIAL TIPS FOR BEGINNERS 



Learning to write machine language programs is a discipline 
whfch is very useful in programming. Since machine language is at 
the same level as the internal workings of the machine, your brain is 
stretched that much further, when trying to organize things in your 
mind and in the VIC-20. 

One of the best ways to learn machine language is to look at 
other people s machine language programs. These are published 
all the time, in magazines and newsletters, even if the article is for a 
different computer that also has the 6502 microprocessor (there 
are many). You should make sure that you thoroughly understand 
the code that you look at. This may require perseverance, 
especially when you see a new technique that you have never 
come across before. This can be infuriating, but if patience prevails, 
you will be the Victor (sorry about that). 

Having looked at other machine language programs, you MUST 
write your own. These may be utilities for your BASIC programs, or 
may be an all machine language program. You should also use the 
utilities that are available, either in your computer, or in a program, 
that aid you in whting, editing, or tracking down errors in a machine 
language program. An example would be the KERNAL, which 
allows you to check the keyboard, print text, control peripheral 
devices like disk drives, printers, modems, etc., manage memory 
and the screen. It is extremely powerful and it is advised strongly 
that it is used (refer to KERNAL section). 

ADVANTAGES OF WRITING PROGRAMS IN 
MACHINE LANGUAGE 

1. Speed^Machfne language is hundreds, and in some cases 
thousands, of times faster than a high level language such as 
BASIC. 

2. Tightness— A machine language program can be made totally 
"watertight, ■ i.e.. the user can be made to do ONLY what the 
program allows, and no more. With a high level language, you are 
relying on the user not 'crashing^' the BASIC interpreter by 
entering, for example, a zero which later causes a: 

?DIVISlON BY ZERO 
ERROR IN LINE 830 
READY. 



16S 



L 



L 



r 



In essence, the computer beiongs to the machine language 
programmer. 

APPROACHING A LARGE TASK 

When approaching a large task in machine language, a certain 
amount of subconscious thought has usually taken place, in 
thinking about how certain processes would be implemented in 
machine language. When the task is started, it is usually a good 
idea to set out on paper block diagrams of memory usage, 
functional modules of code required, and a program flow. Lets say 
that we wanted to write a roulette game in machine language. We 
can outline this as shown below. 

Display title 

Ask if player requires instructions 

YES— display them— Go to START 

NO— Go to START 

START Initialize everything 

MAIN display roulette table 

Take in bats 

Spin wheel 

Slow wheel to stop 

Check bets with result 

Inform ptayer 

Player any money left 

YES— Go to MAIN 

NO— Inform user!, and Go to START 

This is the main outline, which, as each module is approached, 
can then be broken down further. If you look upon a large 
indigestible problem as something that once broken down into 
small enough pieces can all be eaten, then this will enable you to 
approach something that seems impossible, and you will be 
surprised at how swiftly it all falls into place. This process obviously 
improves with practice, but KEEP TRYING. 



169 



MEMORY MAPS 

The following memory maps provide a guide wiiich shows which 
special locations are set aside for use by the VIC's operating 
system , . , and what those locations are used for. 



HEX 



Memory Map 
DECIMAL DESCRIPTION 



0000 





0001-0002 


1-2 


0003-0004 


3-4 


0005-0006 


5-6 


0007 


7 


0008 


8 


0009 


9 


OOOA 


10 


OOOB 


11 


OOOC 


12 


OOOD 


13 


OOOE 


14 


GOOF 


15 


0010 


16 


0011 


17 


0012 


IS 


0013 


19 


•0014-0015 


20-21 


0016 


22 


0017-0018 


23-24 


0019-0021 


25-33 


0022-0025 


34-37 


0026-002A 


38-42 


•002B-002C 


43-44 


•002D-002E 


45-46 


•O02F-003O 


47-48 


*0031-0032 


49-50 


•0033-0034 


51-52 


0035-0036 


53-54 


•0037-0038 


55-56 


0O39-O03A 


57-58 


003B-003C 


59-60 


003D-003E 


61-62 


003F-0040 


63-64 


0041-0042 


65-66 


•0043-0044 


67-68 



Usefu! memory location 



Jump for USR 

Vector for USR 

Float' Fixed vector 

Fixed- Float vector 

Search character 

Scan-quotes flag 

TAB column save 

= LOAD, t=VERIFY 

Input buffer pointer/# subscript 

Default DIM flag 

Type: FF = string. 00 = numeric 

Type: 80 = integer, 00 = floating point 

DATA scan/LIST quote/memory ffag 

Subscript/FNx flag 

= INPUT;$40 = GET;S98 = READ 

ATN sign/Comparison eve! flag 

Current I/O prompt flag 

Integer value 

Pointer: temporary string stack 

Last temp string vector 

Stack for temporary strings 

Utiiity pointer area 

Product area for multiplication 

Pointer: Start of Basic 

Pointer: Start of Variables 

Pointer: Start of Arrays 

Pointer: End of Arrays 

Pointer: String storage {moving down) 

Utility string pointer 

Pointer: Limit of memory 

Current Basic line number 

Previous Basic line number 

Pointer: Basic statement for CONT 

Current DATA line number 

Current DATA address 

Input vector 



170 



r 
[ 



L 



[ 



L 

r 

L 



HEX 



DECIMAL 



DESCRIPTION 



0045-0045 

0047-0048 

0049-004A 

004B-004C 

004D 

004E-0053 

0054-0056 

0057-0060 

*0061 

*0062-0065 

'0066 
0067 
0068 

'0089-006E 
006F 
0070 

0071-0072 

'0073-008A 

007A-007B 

008B-008F 

*0090 

0091 

0092 

0093 

0094 

0095 

0096 

0097 

*0098 

•0099 
*009A 

009B 

009C 

009D 

009E 

009 F 
•OOAO-00A2 

0OA3 

00A4 

00A5 

00A6 

00A7 

OOAB 

00A9 



69-70 Current variable name 

7t-72 Current variable address 

73-74 Variable pointer for FOR. NEXT 

75-76 Y-save; op-save; Basic pointer save 

77 Comparison synnbo! accumulator 

78-83 Misc work area, pointers, etc 

84-86 Jump vector for functions 

87-96 Mtsc numeric work area 

97 Accum#1: Exponent 

98-101 Accum#1: Mantissa 

102 Accum#1:Sign 

103 Series evaluation constant pointer 

104 Accum#l hi-order (overflow) 
105-110 Accum#2: Exponent, etc. 

111 Sign comparison, Acc#1 vs #2 

112 Accum#1 fo-order (rounding) 
113-114 Cassette buffer length/Series pointer 
115-138 CHRGET subroutine (get BASIC char) 
122-123 Basic pointer (within subroutine) 
139-143 RND seed value 

144 Status word ST 

145 Keyswltch Pi A: STOP and RVS flags 

146 Timing constant for tape 

147 Load = 0. Verify = 1 

148 Serial output: deferred char flag 

149 Serial deferred character 

150 Tape EOT received 

151 Register save 

152 How many open files 

153 Input device (normally 0) 

154 Output (CMD) device, normally 3 

155 Tape character parity 

156 Byte-received flag 

157 Direct = $80/RUN=0 output control 

158 Tape Pass 1 error log char buffer 

159 Tape Pass 2 error !og corrected 
160-162 Jiffy Clock (HML) 

163 Serial bit counL^EOI flag 

164 Cycie count 

165 Countdown^ tape write bit count 

166 Pointer: tape buffer 

167 Tape Write Idr count/Read pass/inbit 

168 Tape Write new byte Read error/inbit 
cnt 

169 Write start bit/Read bit err/stbft 



Useful memory location 



171 



HEX 


DECIMAL 


DESCRIPTION ' 


OOAA 


170 


Tape Scan ;Cnt;Lci:End byte assy 


OOAB 


171 


Write lead tength/Rd checksum/parity 


OOAC-OOAD 


172-173 


Pointer: tape buffer, scrolling *- 


OOAE-OOAF 


174-175 


Tape end addresses End of program 


00B0-00B1 


176-177 


Tape liming constants 


*00B2^00B3 


178-179 


Pointer: start of tape buffer 


00 84 


180 


Tape timer (1 = enable); bit cnt 


0085 


181 


Tape EOT RS'232 nesct bit to send 


00B6 


182 


Read cliaracter erroroutbyte buffer 


*0OB7 


183 


# characters in file name 


•00B8 


184 


Current logical file 


•00 B9 


185 


Current secondary address 


*00BA 


186 


Current device 


•00 80^00 BC 


187-188 


Pointer: to file name 


OOBD 


189 


Write shift word/Read input char 


OOBE 


190 


# blocks remaining to Write Read 


00 BF 


191 


Sehal word buffer 


OOCO 


192 


Tape motor interlock 


00C1-00C2 


193-194 


I/O start addresses 


O0C3 0OC4 


195-196 


KERNAL setup pointer 


*00C5 


197 


Current key pressed 


'00C6 


198 


# cliars in keyboard buffer 


*00C7 


199 


Screen reverse flag 


00C8 


200 


Pointer: End-of-line for input 


00C9-00CA 


201-202 


Input cursor log (row, column) 


*00CB 


203 


Which key: 64 if no key 


OOCC 


204 


cursor enable (0 = flash cursor} i 


OOCD 


205 


Cursor timing countdown 1 


OOCE 


206 


Character under cursor *-- 


OOCF 


207 


Cursor in blink phase 


00 DO 


208 


Input from screen/from keyboard 


'00D1-00D2 


209-210 


Pointer to screen line 


*00D3 


211 


Position of cursor on above line 


00 D4 


212 


= direct cursor, else programmed 


*00D5 


213 


Current screen line length 


'00D6 


214 


Row where cursor lives 


00D7 


215 


Last inkey/checksum buffer 


'0OD8 


216 


# of INSERTS outstanding p 


'00D9~00F0 


217-240 


Screen line link table 


0OF1 


241 


Dummy screen link 


00F2 


242 


Screen row marker 


'Q0F3-00F4 


243-244 


Screen color pointer 


00F5-00F6 


245-246 


Keyboard pointer 


00F7-00F8 


247-248 


RS-232 Rev pointer 


00F9-00FA 


249-250 


RS-232 Tx pointer 



Useful memory location 



172 



Hex DECrMAL DESCRIPTION 

•OOFB-OOFE 251-254 Operating system free zero page 

OOFF 255 Basic storage 

01 00-010 A 256-266 Floating to ASCII work area 

01 00-01 3E 256-318 Tape error fog 

01 00-01 FF 256-51 1 Processor stack area 

*0200-0258 512-600 Basic input buffer 

'0259-0262 601-610 Logical fite table 

*0263-026C 611-620 Device # table 

*026D-0276 621-630 Secondary Address table 

*0277-0280 631*640 Keyboard buffer 

'0281*0282 641-642 Start of memory for op system 

'0283-0284 643-644 Top of memory for op system 

0285 645 Serial bus timeout flag 

•0286 646 Current coEor code 

0287 647 Color under cursor 

'0288 648 Screen memory page 

*0289 649 Max sfze of keyboard buffer 

*02SA 650 Key repeat (1 28 = repeat all keys) 

'028B 651 Repeat speed counter 

0280 652 Repeat defay counter 

*028D 653 Keyboard Shfft/Control flag 

028E 654 Ust keyboard shift pattern 

028F-0290 655-656 Pointer: decode logic 

*0291 657 Shift mode switch (0 = enabled, 128- 

locked) 

0292 658 Auto scroll down Hag (0 = on, <>0 = off) 

0293 659 RS-232 control register 

0294 660 RS-232 command register 
0295-0296 661*662 Nonsmndard (Bit time/2-100) 

0297 663 RS-232 status register 

0298 664 Number of bits to send 
0299-029A 665-666 Baud rate (fuff) bit time 
029B 667 RS-232 receive pointer 
029C 668 RS-232 input pointer 
029D 669 RS-232 transmit pointer 
029E 670 RS-232 output pointer 
029F-02A0 671-672 Hoids IRQ during tape operatEons 
02A1'02FF 673-767 Program indirects 

"0300-0301 768-769 Error message link 

0302-0303 770-771 Baste warm start link 

0304-0305 772-773 Crunch Basic tokens link 

0306-0307 774-775 Print tokens link 

0308-0309 776-777 Start new Basic code link 



Useful mefnory location 



173 



HEX 


DECIMAL 


DESCRIPTION 






030A-030B 


778-779 


Get arithmetic element link 




030C 


780 


Storage for 6502 .A 


register 




030 D 


781 


Storage for 6502 .X 


register 




030E 


782 


Storage for 6502 .Y 


register 




030 F 


783 


Storage for 6502 P 


register 




0310-0313 


784-767 


?? 






0314-0315 


788-789 


Hardware (IRQ) interrupt vector (EABFj 


0316-0317 


790-791 


Break interrupt vector 


(FED2) 


0318-0319 


792-793 


NMI interrupt vector 




(FEAD) 


031A-031B 


794-795 


OPEN vector 




(F40A) 


031C'031D 


796-797 


CLOSE vector 




(F34A) 


031E'031F 


798-799 


Set-input vector 




<F2C7) 


0320-0321 


800-801 


Set-output vector 




{F309) 


0322-0323 


802803 


Restore I/O vector 




{F3F3) 


0324-0325 


804-805 


INPUT vector 




{F20E) 


0326-0327 


806807 


Output vector 




(F27A) 


0328-0329 


808-809 


Test-STOP vector 




<F770) 


032A-032B 


810-811 


GET vector 




(F1F5) 


032C^032D 


812-813 


Abort 10 vector 




<F3EF) 


032E-032F 


814-815 


user vector 




(FED2) 


0330-0331 


816^817 


Link to load RAM 




(F549) 


0332-0333 


818-819 


Link to save RAM 




(F685) 


0334-033B 


820-827 


?? 






*033C-03FB 


826-1019 


Cassette buffer 






0400-OFFF 


1024-4095 


3K expansion RAM 


area 




1000-1 DFF 


4096-7679 


User Baste area 






1E00-1FFF 


7680-8191 


Screen memory 






2000-3FFF 


8192-16383 


8K expansion RAM ROM bbck 1 


4000-5 FFF 


16384-24575 


8K expansion RAM/ROM block 2 


6000-7FFF 


24576-32767 


8K expansion RAM ROM block 3 



NOTE; When additional memory is added to block 1 {and 2 and 3), the 
KERNAL relocates the following things for BASIC: 



1000-1 IFF 

1200-? 

9400-95FF 



4096^4607 
4608-? 
37888 = 38399 



Screen memory 
User Basic area 
Color RAM 



8000- 
8000- 
8400- 
8800- 
8C0O 
9000- 



8FFF 
83FF 
87 FF 
8BFF 
-8FFF 
93FF 



32768-36863 4K Character generator ROM 



32768-33791 
33792-33815 
33816-35839 
35840-36863 
36864-37887 



Upper case and graphics 
Reversed upper case and graphics 
Upper and lower case 
Reversed upper and tower case 
10 BLOCK 



L 



r 



Useful memory location 



r 
r 



174 



HEX 



DECIMAL 



DESCBfPTION 



9000-900 F 
9000 

9001 
9002 

9003 

9004 
9005 



36864-36879 
36864 

36665 
36866 

36867 

36868 
36669 



Address of VIC chip registers 

bits 0-6 horizontal centering 

bit 7 sets interlace scan 

vertical centering 

bits 0-6 set # of columns 

bit 7 is part of video matrix address 

bits 1-6 set # of rows 

bit sets S X 8 or 1 6 X 8 chars 

TV raster beam line 

bits 0-3 start of character memory 

(default - 0) 

bits 4-7 [s rest of video address 

(default = F) 

BITS 3,2,1 ,0 CM starting address 









-HEX 


DEC 






0000 ROJVl 


8000 


32768 






0001 


8400 


33792 






0010 


8800 


34816 






0011 


8C00 


35840 






1000 RAM 


0000 


0000 






1001 


xxxx 








1010 


xxxx 


unavail. 






1011 


xxxx 








1100 


1000 


4096 






1101 


1400 


5120 






1110 


1800 


6144 






1111 


1C0O 


7168 


9006 


36870 


horizontal positior 


1 of light 


pen 


9007 


36871 


vertical position of light pe 


m 


9008 


36872 


Digitized value of 


paddte X 


9009 


36873 


Digitized value of paddle ' 


Y 


900A 


36874 


Frequency for oscillator 1 (low) 






(on: 128*255) 






900B 


36875 


Frequency for oscillator 2 (medium) 






(on: 128-255) 






900C 


36876 


Frequency for oscillator 3 (high) 






on: 128-255) 






900D 


36877 


Frequency of noise source 


900E 


36878 


bit 0-3 sets volume of all sound 

bits 4-7 are auxiliary cofor information 


900F 


36879 


Screen and border color register 
bits 4-7 select background color 
bits 0-2 select border cofor 
bit 3 sefects inverted or normal mode 



175 



HEX 


DECIMAL 




DESCRIPTION 


' 


91 10-911 F 


37136-37151 


6522 V!A#1 




9110 


37136 




Port S output register 
(user port and RS-232 lines) 






PIN 


6522 DESCRIPTION EIA 


ABV 




ID 
C 


ID 
PBO 




Sin 




Received data (BB) 




D 


PB1 


Request to Send (CA) 


RTS 




E 


PB2 


Data terminal ready (CD) 


DTR 




F 


PB3 


Ring indicator (CE) 


Rl 




H 


PB4 


Received line signal (CF) 


DCD 




J 


PB5 


Unassigned ( ) 


XXX 




K 


PB6 


Clear to send (CB) 


GTS 




L 


PB7 


Data set ready (CO) 


DSR 




B 


CB1 


Interrupt for Sin (BB) 


Sin 




M 


CB2 Transmitted data (BA) 


Sout 




A 


QND Protective ground (AA) 


GND 




N 


GND Signal ground (AB) 


GND 


9111 


37137 




Port A oulpLit register 
(PAO) BitO^SerialCLKIN 
(PAD Bit 1- Serial DATA IN 
(PA2) Bit 2 = Joy 
CPA3) Bit 3 = Joy 1 
(PA4) Bit 4 = Joy 2 










(PA5) Bit 5 = Lightpea/Fire button ' 








(PA6) Bit 6 = Cassette switch 


sense 








(PA7) Bit 7 = Serial ATN out 




9112 


37138 




Data direction register B 




9113 


37139 




Data direction register A 




9114 


37140 




Timer 1 low byte 




9115 


37141 




Timer 1 high byte & counter 




9116 


37142 




Timer 1 low byle 




9117 


37143 




Timer 1 high byte 




9118 


37144 




Timer 2 low byte 




9119 


37145 




Timer 2 high byte 




911A 


37146 




Shift register 




911B 


37147 




Auxiliary control register 




911C 


37148 




Peripheral control register 
(GA1, CA2, CB1, CB2) 
CA1 = restore key (Bit 0) 










CA2 = cassette motor control (Bits 1 -3) 








CB1 ^interrupt signal for received | 








RS 232 data {Bit 4) 


1 








CB2 = transmitted RS-232 data (Bits 








57) 




911D 


37149 




Interrupt flag register 





176 



HEX 


DECIMAL 


DESCRIPTION 


911E 


37150 


Interrupt enable register 


911F 


37151 


Port A (Sense cassette switch) 


9120-912F 


37152-37167 


6522 VIA#2 


9120 


37152 


Port B output register 
keyboard column scan 
(PB3) Bit 3 ^cassette write line 
(PB7) Bit 7 =Joy 3 


9121 


37153 


Port A output register 
keyboard row scan 


9122 


37154 


Data direction register B 


9123 


37155 


Data direction register A 


9124 


37156 


Timer 1. low byte latch 


9125 


37157 


Timer 1, high byte latch 


9126 


37158 


Timer 1 , low byte counter 


9127 


37159 


Timer 1, high byte counter 
timer 1 is used for the 
60 time second interrupt 


9128 


37160 


Timer 2, low byte latch 


9129 


37161 


Timer 2. high byte latch 


r 91 2A 


37162 


Shift register 


912B 


37163 


Auxiliary control register 


91 2C 


37164 


Peripheral control register 
CA1 Cassette read line (Bit 0) 
CA2 Serial clock out (Bits 1-3) 
CB1 Serial SRQ IN {Bit 4) 
CB2 Serial data out (Bits 5-7) 


912D 


37165 


Interrupt flag register 


912E 


37166 


Interrupt enable register 


912F 


37167 


Port A output register 


9400-96FF 


37888'38399 


location of COLOR RAM with 
additional RAM at bik 1 


9600-97FF 


38400-38911 


Normal location of COLOR RAM 


9800'9BFF 


38912-39935 


1 block 2 


9C00-9FFF 


39936-40959 


10 block 3 


AOOO-BFFF 


40960-49152 


8K decoded block for expansion ROM 


COOO-DFFF 


49152-57343 


8K Basic ROM 


EOOO-FFFF 


57344-65535 


8K KERNAL ROM 



177 



USEFUL MEMORY LOCATIONS 

This is a more in-depth guide to some of the memofy locations 
you can use. 



HEX 

0014-0015 



002B-002C 



DECIMAL 

20-21 



43-44 



002D-002E 45-46 



002F-0030 47-48 



0031-0032 49*50 

0033-0034 51-52 



0037-0038 55*56 



0043-0044 


67-68 


0061-0066 


97*102 


0069-006E 


105-110 


0073-008A 


115-138 


0090 


144 


0098 


152 


0099 


163 



D09A 



164 



DESCRIPTION 

Where BASIC stores integer variables 
used in calculations. The fixed-float and 
float-fixed routines {vectors at 3-4 and 
5-6) use the value in this area. 
The start of the BASIC program in 
memory. Location 43 contains the low 
byle, and location 44 has the high byte. 
To compute the start of BASIC in 
decimal, use the formula; PEEK(43) -^ 
256 * PEEK(44) 

The start of the numeric variables, 
which is usually immediately after the 
end of the BASIC program. 
The start of arrays in memory, usually 
immediately following the numeric vari- 
ables. 

The end of the arrays in memory. 
Bottom of string storage, moving from 
the lop of available memory down to the 
top of arrays. 

The top of free RAM. By lowering this 
value, some RAM can be "protected" 
against BASIC putting values here. 
Jump vector for INPUT statement. 
Floating point accumulator #1 for cal- 
culations. 

Floating point accumulator #2. 
The CHRGET subroutine resides here. 
This routine gets the next BASIC 
character from machine language. 

Status word ST. 

Number of open files. 

Device number for input, normally 

(keyboard). 

Output (CMD) device. nofmaHy 3 

(screen). 



[ 



17a 



HEX 

00A0-00A2 

00B2-0OB3 



00B7 
0OB9 

OOBA 
OOBB-OOBC 



00C5 



DECfMAL 

160-162 

178^179 



183 
185 

186 
187-188 

197 



DESCRIPTION 

3 byte jiffy clock. The T) and Tl$ 
variables are translations of these loca- 
tions. 

Points to the start of the tape buffer Can 
be used as an indirect zero-page jump 
to a routine fn the buffer. 
Number of characters rn filename. 
Which secondary address is currently 
being used. 

Currentdevice number being accessed. 
Points to location of filename in memo- 
ry. 

Current key being held down. There will 
be a 64 here if nothing is held down. If 
more than 1 key is down, the key with 
the highest number on the chart is what 
shows up here. 



# 


key 


# 


key 


# 


key 


# 


key 





1 


16 


none 


32 


space 


48 


Q 


1 


3 


17 


A 


33 


Z 


48 


E 


2 


5 


18 


D 


34 


C 


50 


T 


3 


7 


19 


G 


35 


8 


51 


U 


4 


9 


20 


J 


36 


M 


52 





5 


4- 


21 


L 


37 


t 


53 


@ 


6 


£ 


22 


J 


38 


none 


54 


t 


7 


DEL 


23 


m 


39 


fl 


55 


f5 


8 


^^ 


24 


STOP 


40 


none 


56 


2 


9 


W 


25 


none 


41 


S 


57 


4 


r 10 


R 


26 


X 


42 


F 


58 


6 


I 11 


Y 


27 


V 


43 


H 


58 


8 


12 


1 


28 


N 


44 


K 


60 





13 


P 


29 


, 


45 


: 


61 




14 


* 


30 


/ 


46 


= 


62 


HOME 


15 


RETURN 


31 


Bi 


47 


f3 


63 


f7 


00C6 




198 




Number of characters currently in key- 










boarci buffer. 






00C7 




199 




FJag for 


reverse on/off. A 1 here is on. a 










is off. 








OOCB 




203 




Same as 197. 






00D1-00D2 


209-210 


Address of start of line where cursor Is. 


p 00D3 




211 




Position of cursor on line 




00D5 




213 




Current 


screen line length-wither 21 , 










43, 65. 


or 87. 







179 



HEX 

00D6 

00 DB 



0288 

0289 

028A 

028B 
028D 



DECIMAL 

214 

216 



O0D9-00F0 217-240 



00F3-00F4 


243-244 


OOFB-OOFE 


251-254 


0200-0258 


512-600 


0259-0262 


601-610 


0263-026C 


611-620 


026D-0276 


621-630 


0277-0280 


631-640 



0281-0282 


641-642 


0283-0284 


643-644 


0286 


646 



648 

649 

650 

651 
653 



DESCRIPTION 

Screen row where cursor is. To change 
the cursor position, [ocatbns 201 , 210. 
211, and 214 must be changed. 
N u mber of spaces left In I NS ERT mode . 
POKEmg this to a zero will turn off insert 
mode. 

Screen line link table. A 1 58 means that 
the line is finished at the end of that line, 
and a 30 means that the line continues 
on the next line. 

Pointer to the current space in color 
memory. 

Available locations in zero page. 
BASIC [npiit buffer — where the charac- 
ters being INPUT will go. 
Logic 1 file table for OPEN files. 
Device # table for OPEN files. 
Secondary address table 
Keyboard buffer. If characters are 
POKEd in here and location 198 (# of 
characters in buffer) is changed, it wilt 
be as if the characters were typed from 
the keyboard. 
Start of memory pointer. 
Top of memory pointer. 
Current color code. This holds the color 
number that goes mto color memory 
during PRINT operations. 
Screen memory page. If you want the 
operating system to know where screen 
memory is, this must be changed as well 
as the VIC chip. 

Maximum size of keyboard buffer. If this 
is set greater than 1 0, vital pointers will 
be destroyed. 

Keyboard repeat flag. If this is a 0. only 
cursor controls repeat; if 128, all keys 
repeat. 

This determines how long the VIC waits 
before repealing key. 
Keyboard SHIFT, CTRL, Commodore 
flag. The SHIFT sets the 1 bit, Commo- 
dore sets the 2 bit, and the CTRL sets 
the 4 bit. 

180 



r 



L 



HEX 

0291 



DECIMAL 

657 



0300-0301 768-769 



033C-03FB 828-1019 



DESCRIPTION 

Setting this location to 128 wtll disable 
switching case, and a here enabtes 
the ability to switch. 
This is the jump vector for errors. By 
changing this vector, a routine can 
intercept any error condition. 
Cassette buffer. This Is where data files 
are held before they are INPUT. When 
not using files, this is availabJe for 
POKEing or machine language pro- 
grams. 



181 



THE KERNAL 

One of the toughest problems facing programmers in the 
microcomputer field is the question of what to do when changes are 
made to the operating system of the computer by the company. 
Machine language programs which took much time to develop 
might no longer work, forcing major revisions in the program. To 
alleviate this problem, Commodore has developed a method of 
protecting software writers called the KERNAL 

Essentially, the KERNAL is a standardized JUMP TABLE to the 
input, output, and memory management routines in the operating 
system. The locations of each routine in ROM might change as the 
system is upgraded. But the KERNAL jump table will be changed to 
match, if your machine language routines only use the system 
ROM routines through the KERNAL, it will take much less work to 
modify them. The KERNAL is the operating system of the VIC 
computer. All input, output, and memory management are 
controlled by the KERNAL. 

To simplify the machine language program you write, and to 
make sure that future versions of the VIC operating system don't 
make your machine language programs obsolete, the KERNAL 
contains a jump table for you to use. By taking advantage of the 39 
input/output routines and other utilities accessible from the table, 
not only will you save time, but you will also make it easier to 
translate your programs from one Commodore computer to 
another. 

The jump table is located on the last page of memory, in 
read-only memory. 

To use the KERNAL ]ump table, first you set up the parameters 
that the KERNAL routine needs to work. Then JSR to the proper 
place in the KERNAL jump table. After performing its function, the 
KERNAL transfers control back to your machine language 
program. Depending on which KERNAL routine you are using, 
certain registers may pass parameters back to your program. The 
particular registers for each KERNAL routine may be found in the 
individual descriptions of KERNAL subroutines. 

A good question at this point is why use the jump table at all? Why 
not just JSR directly to the KERNAL subroutine Involved? The jump 
table is used so that if the KERNAL or BASIC is changed, your 
machine language programs will still work. In future operating 
systems the routines may be moved in memory ... but the jump 
table will still work correctly! 



182 



r 



HOW TO USE THE KERNAL 

When writing machine language programs it is often convenient 
to use the routines which are already part of the operating system 
for input/output, access to the system clock, memory management, 
and similar operations. It is an unnecessary duplication of effort to 
write these routines again, so easy access to the operating system 
helps speed machine language programming. 

As mentioned before, the KERNAL is a jump table. This is just a 
collection of JMP instructions to many operating system routines. 

To use a KERNAL routine you must first make all preparations 
that the routine demands . . , if the routine says that you must have 
called another KERNAL routine first, then that routine must be 
called, if the routine expects you to put a number in the 
accumulator, then that number must be there. OthenA^ise your 
routines have little chance of working the way you expect them to 
wori<. 

After all preparations are made, you must call the routine by 
means of the JSR instruction. All KERNAL routines you can access 
are structured as SUBROUTINES, ending with an RTS instruction. 
When the KERNAL routine has finished its task, control wili be 
returned to your program at the instruction after the JSR. 

Many of the KERNAL routines return error codes in the status 
word or the accumulator m case of problems. Good programming 
practice and the success of your machine language programs 
demand that you handle this properly. If you ignore an error return, 
the rest of your program might bomb. 

That's all there is in using the KERNAL— these three steps— 

1. Set up 

2. Call the routine 

3. Error handling 

The following conventions are used in describing the KERNAL 
routines. 

FUNCTION NAME: Name of the KERNAL routine. 
CALL ADDRESS: This is the cail address of the KERNAL routine, 
given in hexadecimal. 

COMMUNICATION REGfSTERS: Registers listed under this 
heading are used to pass parameters to and from the KERNAL 
routines. 

PREPARATORY ROUTINES: Certain KERNAL routines require 
that data be set up before they can operate. The routines needed 
are listed here. 



183 



ERROR RETURNS: A return from a KERNAL routine with the 

CARRY sal indicates that an error was encountered in processing. 

The accumulator will contain the number of the error. 

STACK REQUIREMENTS: This is the actual number of stack 

bytes used by the KERNAL routine. 

REGISTERS AFFECTED: All registers used by the KERNAL 

routine are listed here. 

DESCRIPTION: A short tutorial on the function of the KERNAL 

routine is given here. 

The list of the KERNAL routines follows. 



USER CALLABLE KERNAL ROUTINES 

NAME ADDRESS FUNCTION 



L 



r 
r 





HEX DECIMAL 


ACPTR 


SFFA5 65445 


CHKIN 


SFFC6 65478 


CHKOUT 


SFFC9 65481 


CHRIN 


SFFCF 65487 


CHROUT 


SFFD2 65490 


CIOUT 


SFFA8 65448 


CLALL 


SFFE7 65511 


CLOSE 


SFFC3 65475 


CLRCHN 


SFFCC 65484 


GETIN 


SFFE4 65512 



lOBASE SFFF3 65523 



LISTEN SFFB1 65457 



LOAD 


SFFD5 65493 


MEMBOT 


SFF9C 65436 


MEMTOP 


SFF99 65433 


OPEN 


SFFCO 65472 


PLOT 


SFFFO 65520 


RDTIM 


SFFDE 65502 


READST 


SFFB7 65463 


RESTOR 


SFF8A 65415 


SAVE 


SFFD8 65496 


SCNKEY 


SFF9F 65439 


SCREEN 


SFFED 65517 



Input byte from serial port 
Open channel for input 
Open channel for output 
Input character from channel 
Output character to channel 
Ouput byte to serial port 
Close all channels and files 
Close a specified logical file 
Close input and output channels 
Get character from keyboad queue 
(keyboard buffer) 
Returns base address of I/O de- 
vices 

Command devices on the serial 
bus to LISTEN 
Load RAM from a device 
Read/set the bottom of memory 
Read/set the top of memory 
Open a logical file 
Read/set X,Y cursor position 
Read real time clock 
Read I/O status word 
Restore default 10 vectors 
Save RAM to device 
Scan keyboard 
Return X.Y organization of screen 

184 



r 



r 

L 



NAME ADDRESS 



FUNCTION 



HEX DECIMAL 



SECOND 


$FF93 65427 


Send secondary address after 
LISTEN 


SETLFS 


SFFBA 65466 


Set logical, first, and second ad- 
dresses 


SETMSG 


SFF90 65424 


Control KERNAL messages 


SETNAM 


SFFBD 65469 


Set filename 


SETTIM 


SFFDB 65499 


Set real time clock 


ShIIMO 


SFFA2 65442 


Set timeout on serial bus 


STOP 


SFFE1 65505 


Scan stop key 


TALK 


SFFB4 65460 


Command serial bus device to 
TALK 


TKSA 


SFF96 65430 


Send secondary address after 
TALK 


UDTIM 


SFFEA 65514 


Increment real time clock 


UNLSN 


SFFAE 65454 


Command serial bus to UNLISTEN 


UNTLK 


SFFAB 65451 


Command serial bus to UNTALK 


VECTOR 


SFF84 65412 


Read/set vectored I/O 



B-1. Function name: ACPTR 
Purpose: Get data from the serial bus 
Call address: SFFA5 
Communication registers: .A 
Preparatory routines: TALK ,TKSA 
Error returns: See READS! 
Stack requirements: 13 
Registers affected: ,A, .X 

Description: This is the routine to use to get information from a 
device on the serial bus (Irke the disk). This routine gets a byte of 
data off the serial bus using full handshaking. The data is returned 
in the accumulator. To prepare for this routine the TALK routine 
must have been called first to command the device on the serial bus 
to send data on the bus. If the input device needs a secondary 
command, it must be sent by using the TKSA KERNAL routine 
before calling this routine. Errors are returned in the status word. 
The READST routine is used to read the status word. 

To use this routine: 

0) Command a device on the serial bus to prepare to send data 
to the VIC. 

(Use the TALK and TKSA kernai routines). 

1) Call this routine (using JSR) 

2) Store or othenA/ise use the data. 



18§ 



EXAMPLE; 

Get a byte from the bus 

1} JSR ACPTR 
2) STA DATA 

B-2. Function name: CHKIN 

Purpose: Open a channel for input 

Call address: SFFC6 

Communication registers; .X 

Preparatory routines: (OPEN) 

Error returns: 3,5^6 

Stack requirements: None 

Registers affected: .A, .X 

Description: Any logical file that has already been opened by 
the KERNAL OPEN routine can be defined as an input channel by 
this routine. Naturally, the device on the channel must be an input 
device. Otherwise, an error will occur, and the routine will abort. 

If you are getting data from anywhere other than the keyboard, 
this routine must be called before using either the CHRIN or the 
GETIN KERNAL routines for data Input. If input from the keyboard 
is desired, and no other input channels are opened, then the calls to 
this routine, and to the OPEN routine, are not needed. 

When this routine is used with a device on the serial bus, this 
routine automatically sends the talk address (and the secondary 
address if one was specified by the OPEN routine) over the bus. 

To use this routine: 

0) OPEN the logical file (if necessary; see description above). 

1 ) Load the .X register with number of the logical file to be used. 
2} Call this routine (using a JSR command). 

Possible errors are: 

#3: File not open 

#5: Device not present 

#6: File not an input fi[e 

EXAMPLE: 

; PREPARE FOR INPUT FROM LOGICAL FILE 2 

1) LDX #2 
2} JSR CHKIN 

B-3. Function name: CHKOUT 
Purpose: Open a channel for output 
Call address: SFFC9 
Communication registers: .X 

186 



L 

r 

L 



Preparatory routines: (OPEN) 

Error returns: 3,5 J 

Stack requirements: None 

Registers Affected: .A, .X 

Description: Any logical fife number which has been created by 
the KERNAL routine OPEN can be defined as an output channel. 
Of course, the device you intend opening a channel to must be an 
output device. OthenA/ise, an error will occur, and the routine will be 
aborted. 

This routine must be called before any data is sent to any output 
device unless you want to use the VIC screen as your output 
device- If screen output is desired, and there are no other output 
channels already defined, then the calls to this routine, and to the 
OPEN routine are not needed. 

When used to open a channel to a device on the serial bus, this 
routine will automatically send the LISTEN address specified by the 
OPEN routine (and a secondary address if there was one). 

How to use: Remember: this routine is NOT NEEDED to send 
data to the screen. 0) Use the KERNAL OPEN routine to specify a 
logical file number, a LISTEN address, and a secondary address (if 
needed). 

1} Load the .X register with the logical file number used in the 
open statement. 

2) Call this routine (by using the JSR instruction). 
;DEFINE LOGICAL FILE 3 AS AN OUTPUT CHANNEL 

1 ) LDX #3 

2) JSR CHKOUT 

Possible error returns: 



File not open 
Device not present 
Not an output file 



B-4. Function name: CHRIN 

Purpose: Get a character from the input channel 

Call address: SFFCF 

Communication registers: .A 

Preparatory routines: (OPEN, CHKIN) 

Error returns: See READST 

Stack requirements: None 

Registers affected: .A, -X 

Description: This routine will get a byte of data from the channel 
already set up as the input channel by the KERNAL routine CHKIN. 

t87 



If the CHKIN has not been used to define another input channel, 
data is expected from the keyboard. The data byte is returned in the 
accumulator. The channel remains open after the call. 

Input from the keyboard is handled in a special way. First, the 
cursor is turned on, and will blink until a carnage return is typed on 
the keyboard. Ail characters on the line (up to 88 characters) will be 
stored in the BASIC input buffer. Then the characters can be 
retrieved one at a time by calling this routine once for each 
character. When the carriage return is retrieved, the entire line has 
been processed. The next time this routine is called, the whole 
process begins again, i.e., by flashing the cursor. 

How to use: 

FROM THE KEYBOARD 

1) Call this routine (using the JSR instruction). 

2) Retrieve a byte of data by calling this routine, 
3} Store the data byte. 

4) Check if it is the last data byte {is it a CR ?). If not, go to step 2. 



;Store 00 in the .X register 



r 



EXAMPLE: 


1) 


LDX S#00 


2) 


RD JSR CHRIN 




STA DATA.X 




INX 


3) 


CMP #CR 


4) 


BNE RD 


EXAMPLE: 


JSR CHRfN 


STA DATA 



;store the Xth data byte in the Xth 
;lQcation in the data area. 
;ls it a carriage return? 
;no, get another data byte 



r 



FROM OTHER DEVICES 

0) Use the KERNAL OPEN and CHKIN routines. ' 

1) Call this routine (using a JSR instruction). 

2) Store the data. I 

EXAMPLE: 

JSR CHRIN I 

STA DATA I 

B-5. Function name: CHROUT 
Purpose: Output a character V 



Gal! address: SFFD2 

188 



[ 



CommunEcation registers: .A 

Preparatory routines: (CHKOUT, OPEN) 

Error returns: See READST 

Stack requirements: None 

Registers affected: None 

Description: This routine will output a character to an already 
opened channel- Use the KERNAL OPEN and CHKOUT routines 
to set up the output channel before calling this routine. If this call is 
omitted, data will be sent to the default output device {nunnber 3, on 
the screen). The data byte to be output is loaded into the 
accumulator, and this routine is called. The data is then sent to the 
specified output device. The channel is left open after the call. 

NOTE; Care must be taken when using this routine to send 
data to a sertal device since data wHI be sent to all open output 
channels on the bus. Unless this is desired, all open output 
channels on the serial bus other than the actually intended 
destination channel must be closed by a call to the KERNAL 
close channel routine. 

How to use: 

0) Use the CHKOUT KERNAL routine if needed (see 
description above). 

1) Load the data to be output into the accumulator. 

2) Call this routine. 



EXAMPLE: 



Duplicate the BASIC instruction CMD 4, "A"; 
LOGICAL FILE #4 
OPEN CHANNEL OUT 



LDX #4 

JSR CHKOUT 

LDA #^A 

JSR CHROUT ;SEND CHARACTER 



B-6. Function name: CIOUT 

Purpose: Transmit a byte over the serial bus 

Ca[l address: SFFA8 

Communication registers: .A 

Preparatory routines: LISTEN, [SECOND] 

Error returns: See READST 

Stack requirements: None 

Registers affected: .A 

Description: This routine is used to send information to devices 
on the serial bus. A call to this routine will put a data byte onto the 
serial bus using full serial handshaking. Before this routine is called, 
the LISTEN KERNAL routine must be used to command a device 

189 



on the serial bus to get ready to receive data. (If a device needs a 
secondary address, it must aJso be sent by using the SECOND 
KERNAL routine.) 

The accumulator \s loaded with a byte to handshake as data on 
the serial bus. A device must be listening or the status word will 
return a timeout. This routine always buffers one character. (The 
routine holds the previous character to be sent back.) So when a 
cati to the KERNAL UNLSN routine is made to end the data 
transmission, the buffered character is sent with EOl set. Then the 
UNLSN command is sent to the device. 

How to use: 

0) Use the LISTEN KERNAL routine (and the SECOND routine 
if needed). 

1) Load the accumulator with a byte of data. 

2) Call this routine to send the data byte. 

EXAMPLE: 

;Send an X to the serial bus 
LDA #'X 
JSR CIOUT 

B-7. Function name: CLALL 
Purpose: Close all files 
Call address; SFFE7 
Communication registers: None 
Preparatory routines: None 
Error returns: None 
Stack requirements: 11 
Registers affected: .A, .X 

Description: This routine closes all open files. When this routine 
fs called, the pointers into the open file table are reset, closing all 
files. Also, the routine automatically resets the I/O channels. 

How to use: 

1) Call this routine. 

EXAMPLE: 

;USED AT START OF EXECUTION FOR INITfALfZATION 
JSR CLRCHN ;CLOSE FILES 
JMP RUN ;BEGIN EXECUTION 

B-8* Function name: CLOSE 
Purpose: Close a logical file 
Call address: SFFC3 

190 



L 



r 



r 



CommunjGation registers: .A 
Preparatory routines; None 
Error returns: None 
Stack requirements: None 
Registers affected: .A, .X 

Description: This routine is used to close a logical file after all I/O 
operations have been completed on that file. This routine is called 
after the accumulator is loaded with the logical file number to be 
closed (the same number used when the file was opened using the 
OPEN routine). 

How to use: 

1 ) Load the accumulator with the number of the logical file to be 
closed, 

2) Call this routine. 

EXAMPLE: 

;CLOSE 15 
LDA #15 
JSR CLOSE 



B-9, Function name: CLRCHN 
Purpose: Clear I/O channels 
Call address: SFFCC 
Communication registers: None 
Preparatory routines: None 
Error routines: None 
Stack requirements: 9 
Registers affected: .A, .X 

Description : This routine is called to clear all open channels and 
restore the I/O channels to their original default values. It is usually 
called after opening other I/O channels (like to the disk or tape 
drive) and using them for Input'output operations. The default input 
device is (keyboard). The default output device is 3 (the VIC 
screen). 

If one of the channels to be closed is to the serial port, an 
UNTALK signal is sent first to clear the input channel or an 
UNLISTEN is sent to clear the output channel. By not calling this 
routine (and leaving listener{s) active on the serial bus) several 
devices can receive the same data from the VIC at the same time. 
One way to take advantage of this would be to command the printer 
to TALK and the disk to LISTEN. This would allow direct printing of a 
disk file. 



191 



How to use: 

1) Call this routine using the JSR instruction , 

EXAMPLE: [ 

JSR CLRCHN 

B-10. Function name: GETIN 

Purpose: Get a character from the keyboard buffer 

Call address: SFFE4 

Communication registers: ,A I 

Preparatory routines: None L 

Error returns: None 

Stack requirements: None 

Registers affected: .A, .X 

Description: This subroutine removes one character from the 
keyboard queue and returns it as an ASCII value in the 
accumulator. If the queue is empty, the value returned in the 
accumulator will be zero. Characters are put into the queue 
automatically by an interrupt driven keyboard scan routine which 
calls the SCNKEY routine. The keyboard buffer can hold up to ten ( 

characters- After the buffer \s filled, additional characters are | 

ignored until at least one character has been removed from the 
queue. i 

How to use: I 

1) Call this routine using a JSR instruction 

2) Check for a zero in the accumulator (empty buffer) 

3) Process the data | 

EXAMPLE: 

;WAIT FOR A CHARACTER I 

WAIT JSR GETIN I 

CMP #0 

BEQ WAIT [ 

B-11- Function name: lOBASE ' 

Purpose: Define 1/0 memory page 

Call address: SFFF3 T 

Communication registers; X,Y | 

Preparatory routines: None 

Error returns: None i 

Stack requirements: 2 

Registers affected: .X, .Y ' 

Description: This routine will set the X and Y registers to the 
address of the memory section where the memory mapped I/O [ 

devices are located. This address can then be used with an offset to 1 



192 



L 



access the memory mapped I/O devices in the VIC, The offset will 
be the number of locations from the beginning of the page that the 
desired I/O register is located. The .X register will contain the fow 
order address byte, while the .Y register will contain the high order 
address byte. 

This routine exists to provide compatibility between the VIC 20 
and future models of the VIC. IF the I/O locations for a machine 
language program are set by a call to this routine, they should still 
remain compatibie with future versions of the VIC, the KERNAL and 
BASIC. 

How to use: 

1) Call this routine by using the JSR instruction. 

2) Store the .X and the .Y registers in consecutive iocations. 

3) Load the -Y register with the offset, 

4) Access that I/O location. 

EXAMPLE: 

; Set the data direction register of the user port to (input) 

1) JSR lOBASE 

2) STX POINT ;set base registers 
STY POINT -r1 

3) LDY #2 

4) LDA #0 ;offset for DDR of the user port 
STA {POINT)Y ;Set DDR to 

B-12. Function name: LISTEN 

Purpose: Command a device to LISTEN 

Call Address; SFFB1 

Communication registers: .A 

Preparatory routines: None 

Error returns: See READST 

Stack requirements: None 

Registers affected: .A 

Description: This routine wifl command a device on the serial 
bus to receive data. The accumulator must be loaded with a device 
number between 4 and 31 before cailing the routine. LISTEN will 
OR the number bit by bit to convert to a listen address, then transmit 
this data as a command on the sehal bus. The specified device will 
then go into listen mode, and be ready to accept information. 

How to use: 

1) Load the accumufator with the number of the device to 
command to LISTEN. 

2} Call this routine using the JSR instruction, 

193 



EXAMPLE: 

;COMMAND DEVICE #8 TO LISTEN 
LDA #8 
JSR LISTEN 

B-13, Function name; LOAD 

Purpose: Load RAM from device 

Call address: SFFD5 

Communication registers: A, .X, .Y 

Preparatory routines: SETLFS. SETNAM 

Error returns: 0,4,5,8,9 

Stack requirements: None 

Registers affected: .A, .X, .Y 

Description: This routine will load data bytes from any input 
device directiy into the memory of the VIC. It can aiso be used for a 
verify operation, comparing data from a device with the data 
already in memory, leaving the data stored in RAM unchanged. The 
accumulator (.A) must be set to for a load operation, or 1 for a 
verify. If the input device was OPENed with a secondary address 
(SA) of 0, the header information from device wi II be ignored. In this 
case, the ,X and .Y registers must contain the starting address for 
the load. If the device was addressed with a secondary address of 
0,1, or 2 the data will load into memory starting at the location 
specified by the header. This routine returns the address of the 
highest RAM location which was loaded. 

Before this routine can be called, the KERNAL SETLFS, and 
SETNAM routines must be called. 

How to use 

0) Call the SETLFS, and SETNAM routines. If a relocated load 
is desired, use the SETLFS routine to send a secondary address of 
3. 

1) Set the .A register to for load, 1 for verify. 

2) If a relocated toad is desired, the .X and .Y registers must be 
set to the start address for the load. 

3) Call the routine using the JSR instruction. 

EXAMPLE: 

;LOAD A FILE FROM TAPE 
0) 



L 
[ 
[ 



r 



LDA 


#DEVICE1 ;set device number 




LDX 


#FILENO ;set logical file number 




LDY 


CMD1 ;set secondary address 


JSR 


SETLFS 




LDA 


#NAME1 - NAME ;load .A with number of char- 


"~" 




acters 




;in filename 



194 



LDX 


#<NAME 


;Load .X and .Y with address of 


LDY 


#>NAME 


[filename 


JSR 


SETNAM 




1) LDA 


#0 


;set flag for a load 


2) LDX 


#SFF 


[default start 


LDY 


#SFF 




3) JSR 


LOAD 




STX 


VARTAB 


;end of load 


STY 


VARTAB+1 




JMP 


START 




NAME .BYT 


■FILE NAME 




NAME 1 ; 




* 



B-14. Function name: MEMBOT 

Purpose: Set bottom of memory 

Call address: SFF9C 

Communication registers: .X, .Y 

Preparatory routines: None 

Error returns: None 

Stack requirements: None 

Registers affected: .X, .Y 

Description: This routine is used to set the bottom of the 
memory. If the accumulator carry bit is set when this routine is 
called, a pointer to the (owest byte of RAM will be returned in the .X 
and ,Y registers. On the unexpanded VIC the initial value of this 
pointer is Si 000. If the accumulator carry bit is dear ( = 0) when this 
routine is called, the values of the ,X and .Y registers will be 
transferred to the low and high bytes respectively of the pointer to 
the beginning of RAM. 

How to use: 

TO READ THE BOTTOM OF RAM 

1) Set the carry. 

2) Calf this routine. 

TO SET THE BOTTOM OF MEMORY 

1) Clear the carry. 

2) Call thrs routine, 

EXAMPLE: 

; MOVE BOTTOM OF MEMORY UP 1 PAGE 

SEC :READ MEMORY BOTTOM 

JSR MEMBOT 

INY 

CLC ;SET MEMORY BOTTOM TO NEW VALUE 

JSR MEMBOT 

19S 



L 



B-15, Function name: MEMTOP 

Purpose: Set the top of RAM 

Call address: SFF99 

Communication registers: .X,.Y 

Preparatory routines: None 

Error returns: None J 

Stack requirements; 2 | 

Registers affected: .X, .Y 

Description: This routine is used to set the top of RAM. When 
this routine is called with the carry bit of the accumulator set, the 
pointer to the top of RAM will be loaded into the .X and . Y registers. 
When this routine is called with the accumulator carry bit clear, the 
contents of the .X and .Y registers wilJ be loaded in the top of 
memory pointer, changing the top of memory. 

EXAMPLE: 

;DEALLOCATE THE RS-232 BUFFER 

SEC 

JSR MEMTOP :READ TOP OF MEMORY [ 

DEX I 

CLC 

JSR MEMTOP ;SET NEW TOP OF MEMORY - 

B-16. Function name: OPEN * 

Purpose: Open a logical file 

Call address: SFFCO f 

Communication registers: None 1 

Preparatory routines: SETLFS, SETNAM 

Error returns: 1 ,2,4,5,6 r 

Stack requirements: None | 

Registers affected: .A, .X, „Y 

Description: This routine is used to open a logical file. Once the 
logicai file is set up, it can be used for Input output operations. Most 
of the I/O KERNAL routines call on this routine to create the logical I 

files to operate on. No arguments need to be set up to use this 
routine, but both the SETLFS and SETNAM KERNAL routines | 

must be called before using this routine. \ 

How to use: 

0) Use the SETLFS routine. j 

1) Use the SETNAM routine. 

2) Call this routine. 



196 



EXAMPLE: 

This is an implementation of the BASIC statement: OPEN 

LDA #NAME2-NAME ;LENGTH OF FiLE NAME FOR 

SETLFS 
LDY #>NAME 
JSR SETNAM 

LDA #15 
LDX #8 
LDY #15 
JSR SETLFS 
JSR OPEN 
NAME BYT TO' 
NAME2 

B-17. Function name: PLOT 

Purpose: Set cursor location 

Call address: SFFFO 

Communication registers: .A,X,Y 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: .A, .X. .Y 

Description : A call to this routine, with the accumulator carry flag 
set, loads the current position of the cursor on the screen (in X,Y 
coordinates) into the .X and .Y registers. X is the column number of 
the cursor location (0-21 ) , and Y is the row number of the location of 
the cursor (0-22). A call with the carry bit clear moves the cursor to 
X,Y as determined by the .X and .Y registers. 

How to use: 

READING CURSOR LOCATION 

1) Set the carry flag. 

2) Call this routine. 

3} Get the X and Y position from the .X and .Y registers 
respectively. 
SETTING CURSOR LOCATION 

1) Clear carry flag, 

2) Set the .X and .Y registers to the desired cursor location. 

3) Call this routine. 



197 



EXAMPLE: 

; MOVE THE CURSOR TO 5,5 

LDX #5 

LDY #5 

CLC 

JSR PLOT 

B-18, Function name: RDTIM 

Purpose: Read system clock 

Call address: SFFDE 

Communication registers: .A, .X, .Y 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: .A, ,X, -Y 

Description: This routine is used to read the system clock. The 
dock's resolution is a 60th of a second. Three bytes are returned by 
the routine. The accumulator contains the most significant byte, the 
X index register contains the next most significant byte, and the Y 
index register contains the least significant byte. 

EXAMPLE: 

JSR RDTfM 
STY TIME 
STXTIME + 1 
STATIME^2 

TlfVlE * = * + 3 

B-19. Function name: READST 

Purpose: Read status word 

Call address: SFFB7 

Communication registers: .A 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: .A 

Description: This routine returns the current status of the 10 
devices in the accumulator. The routine is usually called after new 
communication to an I/O device. The routine will give information 
about device status, or errors that have occurred during the 10 
operation. 



198 



r 
[ 
[ 



I 

[ 
r 



The bits returned in the accumulator contain the foflowing 
information {see table befow): 
How to use: 

1) Call this routine. 

2) Decode the information in the .A register as it refers to your 
program. 

EXAMPLE: 

; CHECK FOR END OF FILE DURING READ 

JSR READST 

AND #64 ;check eof bit 

BNE EOF :branch on eof 



' ST 


ST 






Tape 


Bit 


Numeric 


Cassette 


SerialRW 


Verify 


^ Position 


Value 


Read 




+ Load 





1 




Time out 




write 










1 


2 




Time out 




read 










2 


4 


Short block 




Short block 


3 


8 


Long block 




Long block 


I 4 


16 


Unrecoverable 
read error 




Any 
mismatch 


5 


32 


Checksum 
error 




Checksum 
error 


6 


64 


End of file 


EOl line 




r 7 


-128 


End of tape 


Device not 


End of 



present 



tape 



B-20. Function name: RESTOR 

Purpose: Restore default system and interrupt vectors 

Call address: SFF8A 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: .A, .X, .Y 

Description: This routine restores the default values of all 
system vectors used in KERNAL and BASIC routines and 
interrupts. (See appendix D for the default vector contents). The 
KERNAL VECTOR routine is used to read and alter individual 
system vectors. 



199 



How to use: 

1) Call this routine. 

EXAMPLE: 

JSR RESTOR 

B-21. Function name; SAVE 

Purpose: Save memory to a device 

Call address: SFFD8 

Communication registers: ,A, .X, .Y 

Preparaton/ routines: SETLFS, SETNAM 

Error returns: 5,8,9 

Stack requirements: None 

Registers affected: .A, .X, .Y 

Description: This routine saves a section of memory. Memory is 
saved from an indirect address on page specified by the 
accumulator to the address stored in the .X and .Y registers to a 
logical file (an input'output device). The SETLFS and SETNAM 
routines must be used before calling this routine. However, a file 
name is not required to SAVE to device 1 (the cassette tape 
recorder). Any attempt to save to other devices without using a file 
name results in an error. 

NOTE: Device (the keyboard) and device 3 (the screen) cannot 
be SAVEd to. If the attempt is made, an error will occur, and the 
SAVE stopped. 

How to use; 

0) Use the SETLFS routine and the SETNAM routine (unless a 
SAVE with no file name is desired on a save to the tape recorder). 

1 ) Load two consecutive locations on page with a pointer to 
the start of your save (in standard 6502 low byte first, high byte next 
format). 

2) Load the accumulatorwith the single byte page zero offset to 
the pointer. 

3) Load the .X and .Y registers with the low byte and high byte 
respectively of the location of the end of the save- 

4) Call this routine, 

EXAMPLE: 

LDA #1 ;DEVICE = 1 : CASSETTE 

JSR SETLFS 
LDA #0 ;NO FILE NAME 

JSR SETNAM 

LDA PROG ;LOAD START ADDRESS OF SAVE 
STA TXTTAB ; (LOW BYTE) 

LDA PROG + 1 

STA TXTTAB -r 1 (HIGH BYTE) 

200 



L 



[ 



[ 



r 



[ 

L 

r 



LDX VARTAB 
LDY VAR 
TAB + 1 
LDA 
#<TXTTAB 

JSR SAVE 



Load .X WITH LOW BYTE OF END OF SAVE 

.Y WITH HIGH BYTE 

LOAD ACCUMLATOR WITH PAGE OFF- 
SET 



B-22. Fu net ton name: SCNKEY 

Purpose: Scan the keyboard 

Call address: SFF9F 

Communication registers: None 

Preparatory routines: None 

Error returns: None 

Stack requirements: None 

Registers affected: .A, .X, .Y 

Description: This routine will scan the VIC keyboard and check 
for pressed keys. It is the same routine called by the interrupt 
handler. If a key is down, its ASCII value is placed in the keyboard 
queue. 

How to use: 

1) Call this routine 

EXAMPLE: 



GET 



JSR SCNKEY 
JSR GETIN 
CMP #0 
BEQ GET 
JSR GHROUT 



SCAN KEYBOARD 
GET CHARACTER 
IS IT NULL? 
YES. . .SCAN AGAIN 
PRINT IT 



B-23, Function name: SCREEN 

Purpose: Return screen formal 

Can address: SFFED 

Communication registers: X,.Y 

Preparatory routines: None 

Stack requirements: 2 

Registers affected: .X, .Y 

Description: This routine returns the format of the screen, e.g., 
22 columns in .X and 23 lines in ,Y. This routine can be used to 
determine what machine a program is running on, and has been 
implemented on the VIC to help upward compatibility in programs. 

How to use: 

1) Call this routine. 

EXAMPLE: 

JSR SCREEN 



201 



STX MAXCOL ' 

STY MAXROW 

B-24, Function name: SECOND 

Purpose: Send secondary address for LISTEN 

Call address: SFF93 

Communication registers: .A 

Preparatory routines: LISTEN L 

Error returns: See READST 

Stack requirements: None \ 

Registers affected: ,A j 

Description] This routine is used to send a secondary address 
to an I/O device after a call to the LISTEN routine is made, and the r 

device commanded to LISTEN . The routine cannot be used to send 
a secondary address after a call to the TALK routine. - 

A secondary address is usually used to give set-up information to 
a device before 10 operations begin. 

When a secondary address is to be sent to a device on the serial 
bus, the address must first be ORed with $60. 

IHow to use: 

1) Load the accumulator with the secondary address to be 
sent. 

2) Call this routine. 

EXAMPLE: | 

;ADDRESS DEVICE #8 WITH COMMAND (SECONDARY 
ADDRESS) #15 
LDA #8 
JSR LISTEN 
LDA #15 
ORA #60 
JSR SECOND 

B-25. Function name: SETLFS 

Purpose: Set up a logical file 

Call address: SFFBA 

Communication registers: ,A, .X, .Y 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: None 

Description: This routine will set the logical file number, device 
address, and secondary address (command number) for other 
KERNAL routines. 

The logical fife number is used by the system as a key to the file 
table created by the OPEN file routine. Device addresses can 

202 



L 

r 



range from to 30: The following codes are used by the VIC to 
stand for the following CBfyl devices. 



ADDRESS 


DEVICE 





Keyboard 


1 


Cassette #1 


z 


RS-232C device 


3 


CRT display 


4 


Serial Bus printer 


8 


CBM Serial bus disk drive 



Device numbers 4 or greater automatically refer to devices on the 
serial bus. 

A command to the device is sent as a secondary address on the 
serial bus after the device number is sent during the serial attention 
handshaking sequence. If no secondary address is to be sent, the 
.Y index register should be set to 255, 

How to use: 

1) Load the accumulator with the logical file number. 

2) Load the .X index register with the device number. 

3) Load the T index register with the command, 

EXAMPLE: 

For logical file 32, device #4, and no command: 

LDA #32 
LDX #4 
LDY #255 
JSR SETLFS 

B-26. Function name; SETMSG 

Purpose: Control system message output 

Call address: SFF90 

Communication registers: .A 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: ,A 

Description: This routine controls the printing of error and 
control messages by the KERNAL. Either print error messages or 
print control messages can be selected by setting the accumulator 
when the routine is called. FILE NOT FOUND is an example of an 
error message. PRESS PLAY ON CASSETTE is an example of a 
control message. 

Bits 6 and 7 of this value determine where the message will come 
from. If bit 7 is 1 , one of the error messages from the KERNAL will 
be printed. If bit 6 is set, a control message will be printed, 

203 



How to use: 

1) Set accumulator to desired value, 

2) Call this routine. 

EXAMPLE: 

LDA #S40 

JSR SETMSG :TURN ON CONTROL MESSAGES 

LDA #S60 ; 

JSR SETMSG TURN ON ERROR MES- 
SAGES 

LDA #0 

JSR SETMSG ;TURN OFF ALL KERNAL MESSAGES 

B-27. Function name: SETNAM 

Purpose: Set up file name 

Call address: SFFBD 

Communication registers: .A, .X, .Y 

Preparatory routines: None 

Stack requirements: None 

Registers affected: None 

Description: This routine is used to set up the file name for the 
OPEN, SAVE, or LOAD routines. The accumulator must be loaded 
with the length of the file name. The .X and .Y registers must be 
loaded with the address of the file name, in standard 6502 low byte, 
high byte format. The address can be any valid memory address in 
the system where a string of characters for the file name is stored. If 
no file name is desired, the accumulator must be set to 0, 
representing a zero file length. The .X and , Y registers may be set to 
any memory address in that case. 

How to use: 

1) Load the accumulator with the length of the fife name. 

2) Load the .X index register with the low order address of the 
fife name. 

3) Load the .Y index register with the high order address. 

4) Call this routine. 

EXAMPLE: 
LDA #NAME2-NAME ;LOAD LENGTH OF FILE NAME 

LDX #<NAME 
LDY #>NAME 
JSR SETNAM 

B-28, Function name: SETTIM 
Purpose: Set the system ciock 
Call address: SFFDB 

204 



L 
[ 
I 



[ 



Communication registers: .A, .X, .Y 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: None 

Description: A system clock is maintained by an interrupt 
routine that updates the clock every 1/60th of a second (one Jiffy'). 
The clock is three bytes long, which gives it the capability to count 
up to 5,184,000 jiffies {24 hours). At that point the clock resets to 
zero. Before calling this routine to set the clock, the accumufator 
must contain the most significant byte, the .X index register the next 
most significant byte, and the .Y index register the least significant 
byte of the initial time setting (in jiffies). 

How to use: 

1 ) Load the accumulator with the MSB of the 3 byte number to 
set the clock. 

2) Load the .X register with the next byte. 

3) Load the .Y register with the LSB. 

4) Call this routine. 

EXAMPLE: 

;SET THE CLOCK TO 10 MINUTES = 3600 JIFFIES 

LDA #0 ; MOST SIGNIFICANT 

LDX #>3600 

LDY #<3600 ; LEAST SIGNIFICANT 

JSR SETTIM 

B-29. Function name; SETTMO 

Purpose: Set serial bus timeout flag 

Can address: SFFA2 

Communication registers: .A 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: None 

Description : This routine sets the timeout flag for the serial bus. 
When the timeout flag is set, the VIC will wait for a device on the 
serial port for 64 milliseconds, if the device does not respond to the 
VIC'S DAV signal within that time the VIC will recognize an error 
condition and leave the handshake sequence. When this routine is 
called when the accumulator contains a in bit 1\ timeouts are 
enabled. A 1 in bit 7 will disable the timeouts. NOTE: The VIC uses 
the timeout feature to communicate that a disk file is not found on an 
attempt to OPEN a file. 



205 



How to use; 

TO SET THE TIMEOUT FLAG 

1) Set bit 7 of the accumulator to 0. I 

2) Call this routine. L 
TO RESET THE TIMEOUT FLAG 

1) Set bit 7 of the accumulator to 1, [ 

2) Call this routine. 

EXAMPLE: 

;DISABLE TIMEOUT I 

LDA #0 I 

JSR SYSTMO 

B-30. Function name: STOP I 

Purpose: Check if stop key is pressed 

Call address: SFFE1 r 

Communication registers: .A 

Preparatory routines: None 

Error returns: None 

Stack requirements: None 

Registers affected: ,A, .X I 

Description: If the STOP key on the keyboard is pressed when 
this routine is cailed, the Z flag wilj be set. All other flags remain [ 

unchanged. If the STOP key is not pressed then the accumulator I 

will contain a byte representing the last row of the keyboard scan. 
The user can also check for cenain other keys this way. i 

How to use this routine: 

1) Call this routine. ' 

2) Test for the zero flag. 

EXAMPLE: 

JSR STOP 

BNE*-5 ;KEY NOT DOWN r 

JMP READY ; = , ..STOP | 

B-31. Function name: TALK i 

Purpose: Command a device on the senai bus to TALK 
Call address: SFFB4 ' 

Communication registers: .A 

Preparatory routines: None f 

Error returns: See READST 1 

Stack requirements: None 
Registers affected: .A r 



206 



L 



Description: To use this routine the accumuiator must first be 
loaded with a device number between 4 and 30. When called, this 
routine then ORs bit by bits to convert this device number to a talk 
address. Then this data is transmitted as a command on the Sehal 
bus. 

How to use: 

0) 

1) Load the accumulator with the device number 

2) Call this routine, 

EXAMPLE: 

;COMMAND DEVICE #4 TO TALK 

LDA#4 

JSR TALK 

B-32. Function name: TKSA 

Purpose: Send a secondary address to a device commanded to 
TALK ^i 

Call address: SFF96 

Communication registers: .A 

Preparatory routines: TALK 

Error returns: See READST 

Stack requirements: None 

Registers affected: .A 

Description: This routine transmits a secondary address on the 
serial bus for a TALK device. This routine must be called with a 
number between 4 and 31 in the accumulator. The routine will send i 

this number as a secondary address command over the serial bus. 
This routine can only be called after a call to the TALK routine, tt will 
not work after a LISTEN. 

How to use: 

0) Use the TALK routine. 

1) Load the accumulator with the secondary address. 

2) Call this routine. 

EXAMPLE: 

;TELL DEVICE #4 TO TALK WITH COMMAND #7 

LDA #4 ' 

JSR TALK ' 

LDA #7 

JSR TALKSA 



207 



B-33. Functron name: UDTIM 

Call address; SFFEA 

Purpose: Update the system clock I 

Communication registers: None L 

Preparatory routines: None 

Error returns: None r 

Stack requirements: 2 

Registers affected: ,A, .X 

Description: This routine updates the system clock. Normally 
this routine is called by the normal KERNAL interrupt routine every 
1 /60th of a second. If the user program prooesses its own interrupts ' 

this routine must be called to update the time. Also, the STOP key 
routine must be called, if the stop key is to remain functional. T 

How to use: | 

1) Call this routine. 



EXAMPLE: 

JSR UDTIM 



L 



B'34. Function name: UNLSfst 

Purpose: Send an UNLISTEN command 

Call address: SFFAE 

Communication registers: None i 

Preparatory routines: None 1 

Error returns: See READST 

Stack requirements: None r 

Registers affected: .A [ 

Description: This routine commands all devices on the serial 
bus to stop receiving data from the VIC. (i.e., UNLISTEN). Calling p 

this routine results in an UNLISTEN command being transmitted on 
the serial bus. Only devices previously commanded to listen will be ^ 

affected. This routine is normally used after the VIC is finished 
sending data to externa! devices. Sending the UNLISTEN will I 

command the listening devices to get off the serial bus so it can be | 

used for other purposes. 

How to use: I 

1) Call this routine. 

EXAMPLE: 

JSR UNLSN 



208 



B-35, Function name: UNTLK 

Purpose: Send an UNTALK command 

Call address: SFFAB 

Communication registers: None 

Preparatory routines: None 

Error returns: See READST 

Stack requirements: None 

Registers affected: .A 

Description : This routine will transmit an UNTALK command on 
the serial bus. All devices previously set to TALK will stop sending 
data when tin is command is received. 

How to use: 

1) Call this routine. 

EXAMPLE: 

JSR UNTALK 

B*36- Function name: VECTOR 

Purpose: Manage RAM vectors 

Call address: SFF8D 

Communication registers: .X, .Y 

Preparatory routines: None 

Error returns: None 

Stack requirements: 2 

Registers affected: .A, .X, .Y 

Description: This routine manages all system vector jump 
addresses stored in RAM, Calling this routine with the accumulator 
carry bit set will store the current contents of the RAM vectors in a 
list pointed to by the .X and . Y registers. When this routine is called 
with the carry ciear, the user list pointed to by the ,X and . Y registers 
is transferred to the system RAM vectors, NOTE: This routine 
requires caution in its use. The best way to use it is to first read the 
entire vector contents into the user area, alter the desired vectors, 
and then copy the contents back to the system vectors. 

How to use: 

READ THE SYSTEM RAM VECTORS 

1) Set the carry. 

2) Set the .X and ,Y registers to the address to put the vectors, 

3) Call this routine. 



209 



LOAD THE SYSTEM RAM VECTORS 

1) Clear the carry bit. 

2) Set the .X and . Y registers to the address of the vector list in 
RAM tiiat must be loaded 

3} Gall this routine. 

EXAMPLE: 

CHANGE THE INPUT ROUTINES TO NEW SYSTEM 
LDX #<USER 
LDY #>USER 
SEC 

JSR VECTOR 
LDA #<MYINP 
STA USER + 10 
LDA #>MYINP 
STA USER + 11 
LDX #<USER 
LDY #>USER 
CLC 
JSR VECTOR ;alter system 



L 



;read old vectors 
;change input 



L 



USER * = * + 26 



ERROR CODES 



The following is a list of error messages which can occur when 
using the KERNAL routines. If an error occurs during a KERNAL 
routine, the carry bit of the accumulator is set, and the number of the 
error message is returned in the accumulator. 



NUMBER 


MEANING 





Routine terminated by the STOP key 


1 


Too many open files 


2 


File already open 


3 


File not open 


4 


File not found 


5 


Device not present 


6 


File is not an input file 


7 


File Is not an output file 


8 


File name is missing 


9 


Illegal device number 



r 



r 



r 



210 



KERNAL POWER UP ACTIVITIES 



1) KERNAL Checks for the presence of ROM at SAOOO 

The KERNAL looks in memory at SAOOO lor the AUTO START 
ROM SEQUENCE. If this sequence is present, control is 
transferred to the ROM program. 

If this AUTO START ROM code is not present normal system 
initialization continues. 

2) RAM test 

The RAM TEST routine first clears memory from S0000-$OOFF 
and from S0200-S03FF. The cassette buffer pointer is initialized to 
S033C. 

The RAM test starts at location $0400 and works upward, 
checking for the first byte of RAM memory (start-of-RAM). If this 
location is greater than $1 000, then memory is considered bad and 
an error screen is shown. 

Once the test has found the start of RAM it continues, checking 
upward for the first non-RAM location (top-of-RAM). If this focation 
is <$2000, then memory is considered bad and an error screen is 
shown. 

If the top-of-RAM location is greater or equal to S21 00, then the 
screen is set to start at $1000, the bottom-of- memory is set to 
S1200, and the top-of-memory is set to the top-of-RAM location. 

If the top-of-RAM location is less than S2100, then the screen is 
set to start at Si EOO, the bottom-of-memory is set to starl-of-RAM, 
and the top-of-memory is set to SI EOO. 

3) Other Activities 

I/O vectors are set to default values. 

The indirect jump table in low memory is established. 

The GETCHAR routine is created on page zero. 

The screen is then cleared, and the "BYTES FREE' power up 
message is displayed. Control of the system is turned over to 
BASIC and the user. 



m 



VIC CHIPS 



6560 VIDEO INTERFACE CHIP 

The 6560 Video Interface Chip (VIC) is designed for color video 
graphics applications such as low cost CRT terminals, biomedical 
monitors, control system displays and arcade or home video 
games. It provides all of the circuitry necessary for generating color 
programmable character graphics with high screen resolution. VIC 
also incorporates sound effects and A/D converters for use in a 
video game environment. 

FEATURES 

• Fully expandable system with a 16K byte address space 

• System uses industry standard 8 bit wide ROMS and 4 bit wide 
RAMS 

• Mask programmable sync generation, NTSC-6560, PAL-6561 

• On-chip color generation (16 colors) 

• Up to 600 independently programmable and movable back- 
ground locations on a standard TV 

• Screen centering capability 

• Screen grid size up to 192 Horizontal by 200 Vertical dots 

• Two selectable graphic character sizes 

• On-chip sound system including: 

a) Three independent, programmable tone generators 

b) White noise generator 

c) Amplitude modulator 

• Two on-chip 8 bit A/D converters 

• ON -chip DMA and address generation 

• No CPU wait states or screen hash during screen refresh 

• Interlaced. Non-interlaced switch 

• 16 addressable control registers 

• Light gun pen for target games 

• 2 modes of color operation 



r 



212 



PIN CONFIGURATION 

6560 



N.C. C 


1 


COMP COLOR C 


2 


SYNC & LUMIN C 


3 


R/W C 


4 


DB,,C 


5 


OBm C 


6 


DB<,C 


7 


DB, C 


8 


DB, C 


9 


DB^ C 


10 


DBj C 


11 


DB^ C 


12 


DB3 C 


13 


DBj C 


14 


DB, C 


15 


DB^ C 


16 


POTX C 


17 


POT Y C 


18 


COMPSND C 


19 


^ssC 


20 



40 




39 


38 


3 C>^IN 


37 


3 OPTION 


36 


3 P., 


35 


DP., 


34 


:3^3 


33 


3 A, 


32 


3 A, 


31 


:3A, 


30 


3% 


29 


3 A, 


28 


3A^ 


27 


3 A, 


26 


^A, 


25 


:^\ 


24 


^A3 


23 


3 A, 


22 


ZJA^ 


21 


3 A, 



A: Interlace mode: A normal video frame is sent to the TV 60 
times each second. Interlace mode cuts the number of repetitions 
in half. When used v;ith multiplexing equipment, this aflows the VIC 
picture to be blended with a picture from another source. 
To turn off: POKE 36864, PEEK{36864) AND 127 
To turn on: POKE 36864, PEEK(36864) OR 128 
B: Screen origin — horizontal: This determines the positioning 
of the image on the TV screen. The normal value is 5, Lowering the 
value moves the screen to the left, and increasing it moves the 
image to the right. 
To change value: POKE 36864, PEEK{36B64) AND 128 OR X 



213 









VIC CHIP 






LOC 
Hex 


START VALUE-5K VIC 


Bit 
Function 




Binary 


Decimal 




3000 


00000101 




5 




ABBBBBBB 




9001 


00011001 




25 




CCCCCCCG 




9002 


10010110 




150 




HDDDDDDD 




9003 V01D1110 




46 or 176 


GEEEEEEF 




9004 


wvwvw 




V 




6GGGGGGG 




9005 


11110000 




240 




HHHHIIII 




9006 


00000000 









JJJJJJJJ 




9007 


00000000 









KKKKKKKK 




9008 


11111111 




255 




LLLLLLLL 




9009 


11111111 




255 




MMMMMMMM 




900A 


00000000 









NRRRRRRR 




900B 


00000000 









OSSSSSSS 




900C 


00000000 









Pllll 1 1 1 




900D 


00000000 









QUUUUUUU 




900E 


00000000 









WWWWVVVV 




900F 


00011011 




27 




XXXXYZZZ 




A: Interlace mode: = 


off, 


N: Bass sound switch 




! 1 =on 




0: Alto sound switch 




B: Screen origin— horizontal 


P: Soprano sound switch 




C: Screen origin— vertical 




Q: Noise switch 




D: Number of video columns 


R: Bass Frequency 




E: Number of video rows 




S: Alto Frequency 




F: Character size: = 8 


x8, 


T: Soprano Frequency 




1=8x16 




U: Noise Frequency 




G: Raster value 




V: Loudness of sounds 




H: Screen memory location 


W: Auxiliary color 




j 1: Character memory location 


X: Screen color 




J: Light pen— horizontal 




Y: Reverse mode: =■ on, 1 = off 




K: Light pen — vertical 




Z: Border color 




L: Paddle 1 








M: Paddle 2 









C: Screen origin — vertical: This determines the up-down 
placement of the screen image. The normal value is 25. Lower- 
ing this causes the screen to move up by 2 rows of dots for 
each number lowered, and raising it moves the screen down. 

To change value: POKE 36865, X 

D: Number of video columns: Normally, this is set to 22. 



214 



Changing this will change the display accordingly. Numbers over 
27 will give a 27 column screen. The cursor controls are based on a 
fixed number of 22 columns, and changing this number makes the 
cursor controls misbehave. 

To change: POKE 36866, PEEK(36866) AND 128 OR X. 

E: Number of video rows: The number of rows may range from 
to 23, A larger number of rows causes garbage to appear on the 
bottom of the screen. 

To change: POKE 36867. PEEK(36867) AND 129 OR (X*2) 

F: Character size: This bit determined the size of the matrix 
used for each character. A here sets normal mode, in which 
characters are 8 by 8 dots. A 1 sets 8 by 16 mode, where each 
character is now twice as tall. 8 by 16 mode is normally used for 
high resolution graphics, where it is likely to have many unique 
characters on the screen. 

To set 8 by 8 mode: POKE 36867, PEEK(36867) AND 254 

To set 8 by 16 mode: POKE 36867, PEEKC36867) OR 1 

G: Raster value: This number is used to synchronize the light 
pen with the TV picture. 

H: Screen memory location: This determines where in memory 
the VIC keeps the image of the screen. The highest bit in location 
36869 must be a 1 , Bits 4-6 of location 36869 are bits 1 0-1 2 of the 
screen^s address, and bit 7 of location 36866 is bit 9 of the address 
of the screen. To determine the location of the screen, use the 
formula: 

S = 4* (PEEK (36866) AND 128} -r 64* (PEEK (36869) AND 
112) 

Note that bit 7 of location 36866 also determines the location of 
color memory. If this bit is a 0, color memory starts at location 
37888. If this bit is a 1, color memory begins at 38400, Here is a 
formula for this: 

C = 37888 T- 4* (PEEK (36866) AND 128) 

I: Character memory location: This determines where 
information on the shapes of characters are stored. Normally this 
pointer is to the character generator ROM, which contains both the 
upper case graphics or the upper lower case set. However, a 
simple POKE command can change this pointer to a RAM location, 
allowing custom character sets and high resolution graphics. 

To change: POKE 36869, PEEK(36869) AND 15 0R(X*16) 

{See chart on next page,) 

J: Light pen horrzontal: This contains the latched number of the 
dot under the light pen, from the left of the screen. 

K; Light pen vertical: The latched number of the dot under the 
pen, counted from the top of the screen. 



215 



X 

Value 


Location 


Contents 


HEX 


Decimal 





8000 


32768 


Upper case normal characters 


1 


8400 


33792 


Upper case reversed characters 


2 


8800 


34816 


Lower case normal characters 


3 


BCOO 


35840 


Lower case reversed characters " 


4 


9000 


36864 


unavailable 


5 


9400 


37888 


unavailable 


6 


9800 


38912 


VIC chip-unavailable 


7 


9400 


39936 


ROM-unavailable 


8 


0000 





unavailable 


9 






unavailable 


10 






unavailable 


11 






unavailable ' 


12 


1000 


4096 


RAM 


13 


1400 


5120 


RAM 


14 


1800 


6144 


RAM 


15 


1C00 


7168 


RAM 



L 



L: Paddle X: This contains the digitized value of a variable 

resistance (game paddle). The number reads from to 255. 
M: Paddle Y; Same as Paddle X, for a second analog input. 
N: Bass switch: If this bit isaO; no sound is played from Voice 1 . 

A 1 in this bit results in a tone determined by Frequency 1 . 
To turn on: POKE 36874. PEEK(36874) OR 128 
To turn ofl: POKE 36874, PEEK(36874) AND 127 
0: Alto switch; See Bass switch. 
To turn on: POKE 36875, PEEK(36875) OR 128 
To turn off: POKE 36875, PEEK(36875) AND 127 
P: Soprano switch: See Bass switch. 
To turn on: POKE 36876, PEEK(36876) OR 128 
To turn off: POKE 36876, PEEK(36876) AND 127 
Q: Noise switch: See Bass switch. 
To turn on: POKE 36877, PEEK(36877) OR 128 
To turn off: POKE 36877, PEEK(36877) AND 127 
R: Bass Frequency: This is a value corresponding to the 

frequency of the tone being played. The larger the number, the 

higher the pitch of the tone. 
The actual frequency of the sound in cycles per second (hertz) is 

determined by the following formula: 



L 



Frequency = 



Clock 
(127-X) 



216 



X is the number from to 127 that is put into the frequency 
register. !f X is 1 27, then use - 1 for X fn the formula. The value of 
Clock comes from the following table: 



Register 



36874 
36875 
36876 
36877 



NTSC (US TV's) 



3995 
7990 
15980 
31960 



PAL (European) 



4329 
8659 
17320 
34640 



To set: POKE 36874, PEEK{36874) AND 128 OR X 

S: Alto Frequency: This is a value corresponding to the 

frequency of the tone being played. The larger the number, the 

higher the pitch of the tone. 
T: Soprano Frequency: This is a value corresponding to the 

frequency of the tone being played. The larger the number, the 

higher the pitch of the tone. 
To set: POKE 36876, PEEK(36876) AND 128 OR X 
U: Noise Frequency: This is a value corresponding to the 

frequency of the noise being played. The larger the number, the 

higher the pitch of the noise. 
To set: POKE 36877. PEEK(36877) AND 128 OR X 
V: Loudness of sounds: This is the volume control for all the 

sounds playing. is off, and 15 is the loudest sound. 
To set: POKE 36878, PEEK(36878) AND 240 OR X 
W: Auxiliary color: This register holds the color number of the 

auxiliary color. The value can be from to 15. 
To set; POKE 36878, PEEK(36878) AND 15 OR (16*X) 
X: Screen color: A number from to 15 sets the color of the 

screen. 
To set: POKE 36879, PEEK(36B79) AND 240 OR X 
Y; Reverse mode: A 1 in this bit indicates normal characters, 

and a here causes all characters to be displayed as if reversed. 
To turn on reverse mode: POKE 36879, PEEK{36879) AND 247 
To turn off reverse mode: POKE 36879, PEEK{36879) OR 8 
Z: Border color: A number from to 7 sets the color of the 

screen. 
To set: POKE 36879, PEEK(36879) AND 248 OR X 



217 



6522 VERSATILE INTERFACE ADAPTER 

The 6522 Versatile Interface Adapter (VIA) provides the VIC with 
two peripheral ports with input latching, two powerful interval 
timers, and a serial-to-parallel parallel-to-serial shift register. 

Basically, the VIC chip handles Audio/Video input output, and 
the 6522 handles the rest. . .cassette operations, joysticks, 
RS-232, and user port. 

6522 Versatile Interface Adapter Description 



ADDRESS 


DESCRIPTION 


REGISTER 


9110 


Port B 


AAAAAAAA 


9111 


Port A {with handshaking) 


BBBBBBBB 


9112 


Data Direction B 


CCCCCCCC 


9113 


Data Direction A 


DDDDDDDD 


9114 


Timer #1, low byte 


EEEEEEEE 


9115 


Tinner #1. high byte 


FFFFFFFF 


9116 


Timer #1 , low byte to load 


GGGGGGGG 


9117 


Timer #1 , high byte to load 


HHHHHHHH 


9118 


Timer #2, low byte 


llllllli 


9119 


Timer #2, high byte 


JJJJJJJJ 


911A 


Shift Register 


KKKKKKKK 


911 B 


Auxiliary Control 


LLMNNNOP 


911C 


Peripheral Control 


QQQRSSST 


91 ID 


Interrupt Flags 


UVWXYZab 


911E 


Interrupt Enable 


cdefghij 


911 F 


Port A (no handshaking) 


kkkkkkkk 



PORT A I/O REGISTER 

These eight bits are connected to the eight pins wtiich make up 
port B. Each pin can be set for either input or output. 

Input latching is available on this port. When latch mode \s 
enabled the data in the register freezes when the CB1 interrupt flag 
is set. The register stays latched until the interrupt ffag is cleared. 

Handshaking is available for output from this port. CB2 will act as 
a DATA READY SIGNAL. This must be controlled by the user 
program. CB1 acts as the DATA ACCEPTED signal, and must be 
controlled by the device connected to the port. When DATA 
ACCEPTED is sent to the 6522, the DATA READY line is cleared, 
and the interrupt flag is set. 



r 



r 



218 



PORT B I/O REGrSTER 

These eight bits are connected to the eight pins which make up 
port A. Each pin can be set for either input or output. Handshaking is 
available for both read and write operations. Write handshaking is 
similar to that on PORT B. Read handshaking is automatic. The 
CM input pin acts as a DATA READY signal. The CA2 pin (used for 
output) rs used for a DATA ACCEPTED signal. When a DATA 
READY signal is received a flag is set. The chip can be set to 
generate an interrupt or the flag can be polled under program 
control. The DATA ACCEPTED signal can either be a pulse or a DC 
level. It is set low by the CPU and cleared by the DATA READY 
signal 

DATA DIRECTION FOR PORT B 

This register is used to control whether a particular bit in PORT B 
is used for input or output. Each bit of the data direction register 
{DDR} is associated with a bit of port B, If a bit in the DDR is set to 1 . 
the corresponding bit of the port wiJI be an OUTPUT, [f a bit in the 
DDR is 0, the corresponding bit of the port will be an INPUT. 

For example, if the DDR is set to 7, port B will be set up as follows : 



BITS NUMBER 


DDR 


PORT B FUNCTION 





1 


OUTPUT 


1 


1 


OUTPUT 


2 


1 


OUTPUT 


3 





INPUT 


4 





INPUT 


5 





INPUT 


6 





INPUT 


7 





INPUT 



DATA DIRECTION REGISTER FOR PORT A 

This is similar to the DDR for port B» except that it works on PORT 
A. 

E,F,G,H: TIMER CONTROLS 

There are two timers on the 6522 chip. The timers can be set to 
count down automatically or count pulses received by the VIA. The 
mode of operation is selected by the Auxiliary Control register. 

TIMER T1 on the 6522 consists of two 8-bit latches and a 16-btt 
counter. The various modes of the TIfvlER are selected by setting 
the AUXILIARY CONTROL REGISTER (ACR). The latches are 

219 



used to store a 16-bit data word to load into the counter. Loading a 
number into the latches does not affect the count in progress. 

After It is set, the counter will begin decrementing at 1 MHz. When 
the counter reaches zero, an interrupt flag will be set, and the IRQ 
will go low. Depending on how the TIMER is set, either further 
interrupts will be disabled, or it will automatically load the two 
latches into the counter and continue counting. The TIMER can 
also be set to invert the output signal on a peripheral pin each time it 
reaches zero and resets. 

The TIMER locations work differently on reading and writing. 

WRITING TO THE TIMER: 

E: Write into the low order latch. This latch can be loaded into the 
low byte of the 16-bit counter. 

F: Write into the high order latch, write into the high order counter, 
transfer low order latch into the low order counter, and reset the 
TIMER T1 interrupt flag. In other words, when this location is set the 
counter is loaded. 

G: Same as E. 

H: Write into the high order latch and reset the TIMER T1 
interrupt flag. 

READ TIMER T1 

E: Read the TIMER T1 low order counter and reset the TIMER 
T1 interrupt flag. 

F: Read the TIMER T1 high order counter 
G: Read the TIMER T1 low order latch, 
H; Read the TIMER T1 high order latch. 

TIMER T2 

This TIMER operates as an interval timer (in one-shot mode), or 
as a counter for counting negative pulses on PORT B pin 6. A bit In 
the ACR selects which mode TIMER T2 is in, 

WRITING TO TIMER T2 

I: Write TIMER T2 low order byte of latch. 
J: Write TIMER T2 high order counter byte, transfer low order 
latch to low order counter, clear TIMER T2 interrupt flag. 

READING TIMER T2 

I : Read TIMER T2 low order counter byte, and clear TIMER T2 
interrupt flag. 



220 



1 



J: Read TIMER T2 high order counter byte. 
K: SHIFT REGISTER 

A shift register is a register which will rotate itself through the CB2 
pin. The shift register can be loaded with any 8-bit pattern which can 
be shifted out through the CB1 pin, or input to the CB1 pin can be 
shifted into the shift register and then read. This makes it highly 
useful for serial to parallel and parallel to serial conversions. 

The shift register is controlled by bits 2-4 of the Auxiliary Control 
register. 

L,M,N,0,P: AUXILIARY CONTROL REGISTER 

L: TIMER 1 CONTROL 

BIT # 7 6 

One-shot mode (output to PB7 disabled) 

1 Free running mode (output to PB7 disabled) 

1 One-shot mode (output to PB7 enabled) 

1 1 Free running mode (output to PB7 enabled) 

M: TIMER 2 CONTROL 

TIMER 2 has 2 modes. If this bit is 0, TIMER 2 acts as an interval 
timer in one-shot mode. If this bit is 1, TIMER 2 will count a 
predetermined number of pulses on pin PB6. 

N: SHIFT REGISTER CONTROL 

BIT # 4 3 2 

SHIFT REGISTER DISABLED 
1 SHIFT IN (FROM CB1) UNDER CON- 
TROL OF TIMER 2 
1 SHIFT IN UNDER CONTROL OF SYS- 
TEM CLOCK PULSES 

1 1 SHIFT IN UNDER CONTROL OF EX- 

TERNAL CLOCK PULSES 

1 FREE RUN MODE AT RATE SET BT 

TIMER 2 
1 1 SHIFT OUT UNDER CONTROL OF 

TIMER 2 
1 1 SHIFT OUT UNDER CONTROL OF 

SYSTEM CLOCK PULSES 
1 1 1 SHIFT OUT UNDER CONTROL OF 

EXTERNAL CLOCK PULSES 



221 



0: PORT B LATCH ENABLE 

As long as this bit is 0, the PORT B register will directly reflect the 
data on the pins. 

If this bit is set to one, the data present on the input pins of PORT 
A will be latched within the chip when the CB1 INTERRUPT FLAG 
is set. As long as the CB1 INTERRU PT FLAG is set, the data on the 
pins can change without affecting the contents of the PORT B 
register. Note that the CPU always reads the register (the latches) 
rather than the pins. 

Input latching can be used with any of the input or output modes 
available for CB2. 

P: PORT A LATCH ENABLE 

As long as this bit is 0, the PORT A register will directly reflect the 
data on the pins. 

If this bit is set to one, the data present on the input pins of PORT 
A will be latched within the chip when the CM INTERRUPT FLAG 
is set. As long as the CA1 INTERRU PT FLAG is set, the data on the 
pins can change without affecting the contents of the PORT A 
register. Note that the CPU always reads the register (the latches) 
rather than the pins. 

Input latching can be used with any of the input or output modes 
available for CA2. 

Q,R,SJ THE PERIPHERAL CONTROL REGISTER 
Q: CB2 CONTROL 



BIT # 7 6 5 DESCRIPTION 

Interrupt Input Mode 
Independent Interrupt Input fvlode 
Input Mode 

Independent Input Mode 
Handshake Output Mode 
Pulse Output Mode 

Manual Output Mode (CB2 is held 
LOW) 
1 1 1 Manual Output Mode (CB2 is held 
HIGH) 

INTERRUPT INPUT MODE: 

The CB2 interrupt flag (IFR bit 3) will be set on a negative 
(high-to-low) transition on the CB2 input line. The CB2 interrupt bit 
will be cleared on a read or write to PORT B. 

222 



Q 


Q 


Q 


7 


6 


5 

















1 





1 








1 


1 


1 








1 





1 


1 


1 






r 



INDEPENDENT INTERRUPT INPUT MODE: 

As above, the CB2 interrupt flag will be set on a negative 
transition on the CB2 fnput fine. However, reading or writing to 
PORT B does not clear the flag. 

INPUT MODE: 

The CB2 interrupt flag (IFR bit 3) wifl be set on a positive 
(low-to-high) transition of the CB2 line. The CB2 flag will be cleared 
on a read or write of PORT B. 

INDEPENDENT INPUT MODE: 

As above, the CB2 interrupt flag will be set on a positive transition 
on the CB2 line. However, reading or writing PORT B does not 
affect the flag. 

HANDSHAKE OUTPUT MODE: 

The CB2 fine will be set low on a write to PORT 8. It wifl be reset 
high again when there is an active transition on the CB1 line, 

PULSE OUTPUT MODE: 

The CB2 line is set low for one cycle after a write to PORT B. 

MANUAL OUTPUT MODE: 

The CB2 line is held low. 

MANUAL OUTPUT MODE: 

The CB2 line is held high. 

R: CB1 CONTROL 

This bit selects the active transition of the input signal appfied to 
the CB1 pin. If this bit is 0, the GB1 interrupt flag wilf be set on a 
negative transition (high-to-low). if this bit is a 1 , the CB1 interrupt 
flag will be set on a positive (low-to-high) transition. 



CA2 CONTROL 




S 


s 


s 




JIT# 3 


2 


1 


DESCRIPTION 











Interrupt Input Mode 








1 


Independent Interrupt Input Mode 





1 





Input Mode 





1 


1 


Independent Input Mode 



223 



1 








Handshake Output Mode 




1 





1 


Pulse Output Mode 




1 


1 





Manual Output Mode 
LOW) 


(CA2 is hetd 


1 


1 


1 


Manual Output Mode 
HIGH) 


(CA2 is held 



INTERRUPT INPUT MODE: 

The CA2 interrupt flag (IFR bit 0) will be set on a negative 
(high-to-low) transition on the CA2 input line. The CA2 interrupt bit 
will be cleared on a read or write to PORT A. 



INDEPENDENT INTERRUPT INPUT MODE: 

As above, the CA2 interrupt flag will be set on a negative 
transition on the CA2 input line. However, reading or writing to 
PORT A does not clear the flag. 



INPUT MODE: 

The CA2 interrupt flag {IFR bit 0) will be set on a positive 
(low-to-high) transition of the CA2 line. The CA2 flag will be cleared 
on a read or write of PORT A. 

INDEPENDENT INPUT MODE: 

As above, the CA2 interrupt flag will be set on a positive transition 
on the CA2 line. However, reading or writing PORT A does not 
affect the flag. 

HANDSHAKE OUTPUT MODE: 

The CA2 line will be set low on a read or write to PORT A. It will be 
reset high again when there Is an active transition on the CA1 line. 

PULSE OUTPUT MODE: 

The CA2 line is set low for one cycle after a read or write to PORT 
A. 

MANUAL OUTPUT MODE: 

The CA2 line Is held low. 

MANUAL OUTPUT MODE: 

The CA2 line is held high, 

224 



L 



[ 



\ 



T: CA1 CONTROL 

This bit of the PCR sefects the active transition of the Input signal 
applied to the CA1 input pin. If this bit is 0, the CA1 interrupt flag (Bit) 
will be set by a negative transition (high-to-low) on the CA1 pin. If 
this bit is 1 , the CA1 interrupt flag will be set by a positive transition 
(low-to-high). 

There are two registers associated with interrupts: The 
INTERRUPT FUG REGISTER (IFR) and the INTERRUPT 
ENABLE REGISTER (lER). The IFR has eight bits, each one 
connected to a register In the 6522. Each bit in the IFR has an 
associated bit in the lER. The flag is set when a register wants to 
interrupt. However, no interrupt will take place unless the 
corresponding bit in the lER is set. 

UVWXYZab: INTERRUPT FLAG REGISTER 

When the flag is set, the pin associated with that flag is 
attempting to interrupt the 6502. Bit U is not a normal flag. It goes 
high if both the fiag and the corresponding bit in the INTERRUPT 
ENABLE REGESTER are set. It can be cleared only by clearing all 
the flags in the IFR or disabling all active interrupts in the lER. 





SET BY 


CLEARED BY 


u 


IRQ STATUS 




V 


TIMER 1 time-out 


Reading TIMER 1 low order 
counter and writing TIMER 
1 high order latch 


w 


TIMER 2 time-out 


Reading TIMER 2 low order 
counter and writing TIMER 
2 high order counter 


X 


CB1 pin active transition 


Reading or writing PORT B 


Y 


CB2 pin active transition 


Reading or writing PORT B 


z 


Completion of 8 shifts 


Reading or writing the shift 
register 


a 


CA1 pin active transition 


Reading or writing PORT A 
(BBBBBBBB in above 
chart) 


b 


CA2 pin active transition 


Reading or whting PORT A 
(BBBBBBBB in above 
chart) 



cdefghij: tNTERRUPT ENABLE REGISTER 
c: ENABLE CONTROL 

If this bit is a during a write to this register, each 1 in bits 0-6 

225 



clears the corresponding bit in the lER. If this bit is a 1 during this 
register, each 1 in bits 0-6 will set the corresponding lER bit. . 

d TIMER 1 time-out enable I 

e TIMER 2 time-out enable ^ 

f CB1 interrupt enable 

g CB2 interrupt enable f 

h Shift interrupt enable 1 

I CA1 interrupt enable 
j CA2 Interrupt enable 

kkkkkkkk: PORT A ' 

This is similar to BBBBBBBB, except that the handshaking lines 
{CA1 and CA2) are unaffected by operations on this port. 



L 



226 



L 

r 



L 
L 



[ 

L 



L 



THE USER PORT 



The user port is meant to connect the VIC to the outside world. By 
using the lines available at this port, you can connect the VIC to a 
printer, a Votrax Type and Talk, a MODEM, a second joystick, even 
another computer. 

The port on the VIC is directly connected to one of the 6522 VIA 
chips. By programming, the VIA will connect to many other devices. 

Port Pin Description 



PIN # 



DESCRIPTION 



UPPER SIDE 

1 GROUND 

2 +5V 

3 RESET 



JOYO 



5 


J0Y1 


6 


JOY 2 


7 


LIGHT 




PEN 


8 


CASSElib 




SWITCH 


9 


SERIAL 




ATN IN 


10 


9VAC 


11 & 


9VAC 


12 


GND 


BOTTOM SIDE 


A 


GND 


B 


CB1 


C 


PBO 


D 


PB1 


E 


PB2 


F 


PB3 


H 


PB4 


J 


PB5 


K 


PB6 



(100mA MAX.) 

By grounding this pin, the VIC will do a 

COLD START, resetting completely 

and erasing any program in memory. 

This pin is connected to joystick 

switch (See GAME PORT). 

(See GAME PORT,) 

(See GAME PORT.) 

This pin also acts as the input for the 

joystick FfRE button (See GAME PORT). 

This pin is connected to the SENSE cassette 

switch line. 

This pin is connected to the ATN IN line 

of the serial bus. 

Connected directly to the VIC transformer 



The VIC gives you complete control over 
Port Bon VIAchip#1. Eight lines for input or 
output are available, as weEi as 2 lines for 
handshaking with an outside device. The I/O 
lines for PORT B are controiled by two 
locations. One is the PORT itself, and is 
located at 371 36 (S911 HEX). Naturally you 
PEEK It to read an INPUT, or POKE St to set 
an OUTPUT. Each ot the eight 10 lines can 



229 



L 


PB7 


M 


CB2 


N 


GND 



be set up as either an INPUT or an OUTPUT 
by setting the DATA DIRECTION REGIS- 
TER properly. It is located at 37138 (S91 12 
hex). 

Each of the eight lines in the PORT has a BIT in the 8 bit DATA 
DIRECTION REGISTER (DDR) which controls whether that line 
will be an input or an output. If a bit in the DDR is a ONE, the 
corresponding line of the PORT will be an OUTPUT. If a bit in the 
DDR is a ZERO, the corresponding line of the PORT will be an 
INPUT, For example, if bit 3 of the DDR is set to 1 , then iine 3 of the 
PORT will be an output. As another example, if the DDR is set like 
this: 

BIT #: 76543210 

VALUE: 1110 

You can see that lines 5,4, and 3 will be outputs since those bits are 
ones. The rest of the lines will be inputs, since those lines are zeros. 

To PEEK or POKE the USER port, it is necessary to use both the 
DDR and the PORT itself. 

Remember that the PEEK and POKE statements want a number 
from 0-255. The numbers given in the example must be translated 
into decimal before they could be used. (The value would be: 2- + 2* 
+ 2^ - 32-h16-r8 =56. See Section 1 on numbers for more 
details.} 

The other two lines, CB1 and CB2 are different from the rest of 
the USER PORT. These two lines are mainly for HANDSHAKING, 
and are programmed differently from port B. 

Handshaking is needed when two devices communicate. Since 
one device may run at a different speed than another device, it is 
necessary to give the devices some way of knowing what the other 
is doing. Even when the devices are operating at the same speed, 
handshaking is necessary to let the other know when data is to be 
sent, and if it has been received. Both the CB1 and CB2 lines have 
special characteristics which make them well suited for handshak- 
ing. 

CB1 is usually used as an input (except under SHIFT REG ISTER 
control). CB2 can be used both for input and output, but is usually 
used for output. 

It is not possible to read CB1 directly. CB1 is designed to set a 
flag (bit 4) in the INTERRUPT FLAG register (located at 37149 or 
S911 D HEX) when a transition occurs on the CB1 line. Bit 4 of the 
PERIPHERAL CONTROL REGISTER (PCR) located at 37148 
(S911C hex} determines whether CB1 will set the flag on a 



230 



fow-to-high transition or on a high-to-low transition. Once the CB1 
flag is set, it will stay set until you clear it by a Peek or POKE to 
PORT B which resets the CB1 ffag bit. If bit 4 in the INTERRUPT 
ENABLE register is set, and interrupts are enabied, the transition 
will also cause an INTERRUPT REQUEST (IRQ). 

CB2 IS controlled by the PCR. Bits 7,6 control whether CB2 will 
be an input or an output. Bit 5 controls the setting of CB2. 



BIT# 7 


6 


5 


DESCRIPTION 











Interrupt Input Mode 








1 


Independent Interrupt Input Mode 





1 





Input Mode 





1 


1 


Independent Input Mode 


1 








Handshake Output Mode 


1 





1 


Pulse Output Mode 


1 


1 





Manual Output Mode (CB2 is held LOW) 


1 


1 


1 


Manual Output Mode (CB2 is held HIGH) 



INTERRUPT INPUT MODE: 

The GB2 interrupt flag (IFR bit 3) will be set on a negative 
(high-to-low) transition on the CB2 input line. The CB2 interrupt bit 
will be cleared on a read or write to PORT B. 

INDEPENDENT INTERRUPT INPUT MODE: 

As above, the CB2 interrupt flag will be set on a negative 
transition on the CB2 input line. However, reading or writing to 
PORT B does not dear the flag. 

INPUT MODE: 

The CB2 interrupt flag (IFR bit 3) will be set on a positive 
(low-to-high) transition of the 082 line. The CB2 flag will be cleared 
on a read or write of PORT B. 

INDEPENDENT INPUT MODE: 

As above, the GB2 interrupt flag wilf be set on a positive transition 
on the CB2 line. However, reading or writing PORT B does not 
affect the flag. 

HANDSHAKE OUTPUT MODE: 

The CB2 line will be set low on a whte to PORT B. It will be reset 
high again when there is an active transition on the CB1 line. 



231 



PULSE OUTPUT MODE: 

The CB2 line is set low for one cycle after a write to PORT B. 

MANUAL OUTPUT MODE: 
The CB2 line is held low. 

MANUAL OUTPUT MODE: 

The CB2 line is held high. 

MORE MUSIC FOR THE VIC 

Now that you know about the USER PORT, there is little surprise 
left- Up to now, the VIC has had 4 musical voices . . . three music 
registers and a white noise register. By connectmg a small amplifier 
and speaker to the USER PORT, and doing a little programming, 
you can get another musical voice. 

THEORY 

Most music is made up of square waves of different amplitudes 
and frequencies. One of the functions of the 6522 chip is to 
generate square waves through the CB2 line. If we connect the 
CB2 line to a speaker, we will be able to hear the square waves 
generated by the VIC. 

NOTE: Connecting a speaker directly to CB2 may damage your 
VIC. You must connect the speaker through an amplifier to protect 
the VfC. 

PARTS NEEDED 

1. Small battery powered speaker amplifier 

2. User Port Connector (12 position, 24 contact edge connector 
with .156" spacing 

3. Wire 

CONNECTING TO YOUR VIC 

1 . Wire the GROUND of the amplifier to the G ROUND of the USER 
PORT (pin N). 

2. Wire the SIGNAL of the amplifier to the CB2 output of the USER 
PORT (pin M). 

You are now ready to add your other voice through a BASIC 
program. 



232 



r 



r 



BASIC PROGRAM STEPS: 

1. Set the 6522 shift register to free running mode by: 
POKE 37147,16 

2. Set the shift rate by: 

POKE 37144,0 where C is an integer from to 255 
C \s the note to be played. 

3. Load the shift register by: 

POKE 371 46. D where D =15, 51, or 85 for a square wave. 
This step sets the octave for the note. 

This step must be done last, since as soon as it is set, the VIC 
starts generating the square waves. 

The frequency of the square wave can be found by the following 
formula: 

FREQUENCY = 500000 Hz Where D1 =8 when D=15 

(C + 2) (D1) ^^ = "^ wh^" D - 51 

D1=2whenD-85 

When you are in this mode, the VIC wiil not read or write to 
cassette. To restore normal operations, you must: 

POKE 37147,0 

The following program demonstrates music using this method. 
By hitting a letter the note will be played. 

10 PRINT ^'MUSICAL USING CB2.'^ 

15 PRINT "HIT + TO GO UP AN OCTAVE" 

16 PRINT "HIT - TO GO DOWN AN OCTAVE" 

17 PRINT: PRINT "USE E TO EXIT." 

20 POKE37147,16:DIMA{30):FORK= 1TO30:READA(K): 

NEXT 
40 GETAS:IFA$ = ""THEN40 
42 IFA$ = "E" THEN POKE37147,0:END 
45 IFAS = ^'-^" THEN SF= SF-(SF<2):GOTO40 
50 IFAS = "-" THEN SF= SF-^(SF<0):GOTO40 
60 A = 8-ASC(AS)H-64:IF A>7 OR A<1 THEN 40 
70 POKE 37144,A(A-(SF-1)*10-(SF = 2r20) 
80 POKE371 46. - (SF = OflS - (SF = 1 )*51 - {SF = 2r85 
90 GOTO40 

100 DATA 59.61.65,69.7377,82.87,90,93 
110 DATA 99.104,111,117,120,124.132,140,149,157 
120 DATA 167.177,182,188,199,211,224.237,244.251 



233 



THE SERIAL BUS 



The serial bus is a daisy chain arrangement designed to let the 
VIC communicate with the VIC-1540 DISK DRIVE and the 
V!C'1515 GRAPHICS PRINTER. Up to 5 devices can be 
connected to the serial bus at one time. 

All devices connected on the serial bus will receive all the data 
transmitted over the bus. To allow the VIC to route data to its 
intended destination, each device has a bus ADDRESS. By using 
this device address, the VIC can control access to the bus. 
Addresses on the serial bus range from 4 to 31. 

The VIC can COMMAND a particular device to TALK or LISTEN . 
When the VIC commands a device to TALK, the device will begin 
putting data onto the serial bus. When VIC commands a device to 
LISTEN, the device addressed will get ready to receive data (from 
the VIC or from another device on the bus). Only one device can 
TALK on the bus at a time; otherwise the data will collide and the 
system will crash in confusion. However, any number of devices 
can LISTEN at the same time to one TALKER. 

COMMON SERIAL BUS ADDRESSES 

Number Device 

4 or 5 VIC-1515 GRAPHIC PRINTER 

8 VIC DISK DRIVE 

Other device addresses are possible. Each device is wired to an 
address. Certain devices (like the VIC printer) provide a choice 
between two addresses for the convenience of the user. 

The SECONDARY ADDRESS is to let the VIC transmit set up 
information to a device. For example, to OPEN a connection on the 
bus to the printer, and have it print in UPPER/LOWER case, use the 
following: 

OPEN 1,4 J 

Where 1 is the logical file number (the number you PRINT# to) 

4 is the ADDRESS of the printer 
and 7 is the SECONDARY ADDRESS that tells the printer to 
go into UPPER LOWER case mode. 

SERIAL BUS PINOUTS 

PIN # 

1 SERIAL SRQ IN 

2 GND 



234 



3 SERIAL ATN IN/OUT 

4 SERIAL CLK IN/OUT 
6 SERIAL DATA IN/OUT 
6 NO CONNECTION 

SERIAL SRQ IN: (SERIAL SERVICE REQUEST IN) 

Any device on the serial bus can bring this signal LOW when it 
requires attention from the VIC. The VIC will then take care of the 
device, 

SERIAL ATN JN/OUT: (SERIAL ATTENTION IN/OUT) 

The VIC uses this signal to start a command sequence for a 
device on the serial bus. When the VIC brings this signal LOW, all 
other devices on the bus starl listening for the VIC to transnnit an 
address. The device addressed must respond in a preset period of 
time; othenA/ise the VIC will assume that the device addressed is 
not on the bus, and will return an error in the STATUS WORD. 

SERIAL CLK IN/OUT: (SERIAL CLOCK IN/OUT) 

This signal is used for timing on the serial bus. 

SERIAL DATA IN/OUT: 

Data on the serial bus is transmitted one bit at a time on this line. 



235 



USING THE VIC GRAPHIC 
PRINTER 



The VIC Graphic Printer connects to the serial port on the back of 
the VIC and is used for printing out listings of programs, statistical 
data, graphs, charts and even graphic plotting . The VIC printer can 
also be used with the VlCWriter wordprocessing cartridge to write 
reports, transcribe school notes, write letters and other documents. 

Programmers use dot matrix printers to make paper copies of 
program listings which are easier to debug and edit on paper. It's 
also helpful to print out text and graphics displayed on the screen 
through what is called a "screen dump to the printer." 

Several short programs which demonstrate the use of the printer 
are included below. WeVe even included a special "typing" 
program which lets you use the VIC as a typewriter to enter words 
or graphics directly from the keyboard. 

LISTING DATA ON YOUR VfC PRINTER 

The proper format for printing out a listing of a program which 
resides in memory is to enter the following line and type RUN. 

OPEN4.4:CMD4:LIST 

Note that rf you have left a printer file open you will get a FILE 
OPEN error. If this happens, type CL0SE4, and hit RETURN. Then 
retype the above. 

If you're developing a new program, you probably want to list out 
each revision so you can edit it on paper before proceeding to the 
next step. A good way to do this is to (1 ) type in your program, (2} 
include an END statement at the end of the program, (3) include the 
list-to-printer instruction at a high line number above the END 
statement, and (4) RUN the line with the printer instruction 
whenever you want to list out the program. This technique lets you 
RUN and edit your program normally, but it saves you the trouble of 
having to type in the LIST line every time you want a listing on 
paper. In the following exampie, if you type RUN, the program will 
print out line 10. If you type RUN 5000, the program will list to the 
printer. 

10 PRINTTHIS IS MY PROGRAM' 

20 END 

5000 CL0SE4; OPEN4,4:CMD4:LIST 



236 



r 



r 
[ 



L 



VIC GRAPHIC PRINTER COMMAND CHART 



The VIC Graphic Printer tnciudes a strong "language" of special 
print commands, as described in the following chart. Insert the 
CHRS command to use these commands: 

10 PRINT#4,CHR$ (14) "PUT DATA HERE^ 



PRINT COMMAND 



DESCRIPTION 



CHRS (10) 
CHRS (13) 
CHR$ (8) 
CHRS (14) 
CHRS {15} 

CHRS (16) 
CHRS (27) 



CHRS (26) 
CHRS (145) 
CHRS (17) 
CHRS (18) 
CHRS (146) 



Line feed after printing 

Carriage return 

Graphic mode command 

Double width characters 

Standard character mode 

(type this to get back to normal) 

Print start position addressing 

When followed by the CHRS (16) position 

code this command is used to specify a 

start position by dot address (in contrast to 

character address) 

Repeat graphic select command 

Cursor up (Upper case) mode 

Cursor down (Upper/Lower case) mode 

Reverse field on command 

Reverse field off command 



PRINTING DOT PROGRAMMABLE GRAPHICS 

There are two fairly easy ways to print your own graphic 
characters — in other words, "dot programmable graphics." One 
way is to purchase Commodore's PROGRAMMABLE CHARAC- 
TER SET & GAMEGRAPHICS EDITOR, which is avaiiable on tape 
at an economicaf price. The other way is to define your own 
characters TO THE PRINTER using a character matrix of 7x7 
dots. You have to define each character in terms of its binary code 
and the best way to do this is to use DATA statements. Look at the 
foilowing matrix and decide which dot pattern you would like to 
design. A sample is shown. Now, to print this out, you have to add 
up the binary values for each coiumn. Count zero for empty blocks, 
count the value shown on the left of the pattern if there is a dot there, 
then add up all the values for the column. The total of each column 
is shown at the bottom in our sample on page 238. 



237 



PHINTING IN DIRECT MODE 

You can use your printer like a typewriter by printing in the DIRECT 
MODE. In this mode, the printer prints everything between the 
quotation marks including graphics and reversed characters. 
Here's a sample ot how it's done: 

SAMPLE PROGRAM 



L 

r 





^ 7 








'•'« 




















m 















• 








7 


^ 


















9 








9 






16 




J 






m 


# 




32 






•• 








S4 




2A 


M 


65 


65 


b* 


34 





+ T28 


156 


1S2 


iS3 


193 


182 


162 


123 





le rRTR136.lS2il93.193.182.162 

20 F0RI»1T06 

30 RERDfl 

40 fi*=R*+CHR$<R) 

50 NEXT 

60 0PEH4.4 

70 F0RI-1T04 

80 Pf?INTM,CHR»<8>R»; 

90 PRINT#4.CHR*<i5>" COMHODORE" 

100 NEXT 

After typing RUN, you get this result: 

C COMMODORE 
C COMMODORE 
C COMMODORE 
COMMODORE 



[ 



YOU TYPE: 



SCREEN DISPLAYS: 



PRINTER 
PRINTS: 



OPEN4,4 



No response. 



OPEN4,4 

READY. 
CIVID4 No Response. READY. 

PRINT#4, "HELLO. PRINT#4, -HELLO. HELLO.LOVE 
LOVE '■ LOVE 

CL0SE4 CL0SE4 READY. 

READY. 

PRINTING DOUBLE WIDTH CHARACTERS 

Double width characters have many applications, from enlarging 
graphics on paper to printing bold face headlines and titles. The 
following program demonstrates how to use CHRS (14) to print 
double spaced letters. . .and also shows how to get back to normal 
letters by typing CHRS (15). 

10 OPEN4,4:PRINT#4,CHRS (14) "DOUBLE LETTERS" 
20 PRINT#4, "STILL DOUBLE" 



238 



L 



30 PRfNT#4, CHR$ (15) ''NORMAL AGAIN" 
40 PRINr'HELLO AGAIN IN NORMAL MODE'^ 

{It you change PRINT#4 to CMD4 in line 30 it still works.) 

PRINTING REVERSE CHARACTERS 

You may use the codes shown in the VIC Graphic Printer 
Command chart to tell the printer to print reverse characters by 
including these lines: 

Reverse On: 10 0PEN4.4: PRINT#4,CHRS (18) 

Print Info: 20 PRINT#4, ^ VIC 20 ' 

Reverse Off: 30 PRiNT#4,CHRS(146) 

Normal Again: 40 PRINT#4;VIC 20" 

PRINTING WHAT IS DISPLAYED ON THE SCREEN 

Type and run these lines as a program or as a subroutine to get a 
printout of what is displayed on the screen from your program. 
When you use it, add a line or command that says: GOSUB 60000 
and enter this program as shown. Lines 10 through 25 are included 
to give you an example of how this can wori< and are not part of the 
screen dump program. Your own program would have different 
commands here, of course. 

You can type different "screens" of information in the course of a 
program. One way to do this is to add a line in your program that 
would scan the keyboard and look for a function key. Try adding this 
line as the first line of your program: 1 GETXS:IFXS = CHRS (1 33) 
THEN GOSUB60000. This line lets you print out whatever is on the 
screen whenever you hit Function Key 1. 

10 PRINT 'SAMPLE'' 

20 GOSUB60000 

25 END 

60000 REM SCREEN COPY 

60010 RS = CHRS(145):VS = CHRS(146):OPEN4, 

4:PRINT#4:G-PEEK{64e)"256:PRINT#4.RSFORP = 

GTOG - 505 

60020 C-PEEK(P):C$- ' ■:lF{P-G)/22-INT((P-G)/22)THEN 

PRINT#4.CHRS{8)-CHRS(13)-CHR$(15); 

60030 IF0128THENC- 0-128:0$ = CHRS(18) 

60040 IFC<320RC>95THENC - C + 64:GOTO60060 

60050 IFC:>63ANDC<96THENC = 4- 1 28 

60060 CS-CS-CHRS(C):IFLEN(CS)>1THENCS = CS- 

VS-RS 

60070 PRINT#4,CS; :NEXT:PRINT#4:CL0SE4:RETURN 



239 



r 



PRINTING VfC PROGRAMS 

Many VIC programs work with the VIC Graphic Printer. Our 
Home Calculation progranns, for example, let you hit a function key L 

to print out a screen full of data for your records and files. This is 
helpful in keeping paper records of home inventory lists, personal I 

budget information, etc. These programs give you the ability to j 

compile, calculate, edit and delete information on your VIC 20, then 
print out the results. A powerful combination! I 



1 

L 



240 



VIC EXPANSION PORT 



THE EXPANSION CONNECTOR 

The expansion connector is a 48 pin (24.24) female edge 
connector on the back of the VIC. With the VIC facing you. the 
expansion connector Is on the far right of the back. To use the 
connector, a 44 pin (22/22) male edge connector is required. 

The expansion bus is arranged as fallows: 

t 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 13 19 20 2122 



ABGDEFHJ KLMNPRSTUVWXYZ 



PIN# 


TYPE 


1 


GND 


2 


CD0 


3 


CD1 


4 


CD2 


5 


CD3 


6 


CD4 


7 


CDS 


8 


CD6 


9 


CD7 


10 


BLK1 


11 


BLK2 



PIN# 


TYPE 


12 


BLK3 


13 


BLK5 


14 


RAMI 


15 


RAM2 


16 


RAMS 


17 


VR/W 


18 


CRAA/ 


19 


IRQ 


2^ 


NC 


21 


+ 5V 


22 


GND 



PIN# 


TYPE 


A 


GND 


B 


CA0 


C 


CA1 


D 


CA2 


E 


CA3 


F 


CA4 


H 


CAS 


J 


CA6 


K 


CA7 


L 


CAS 


M 


CA9 



PIN» 


TYPE 


N 


CA10 


P 


CA11 


R 


CA12 


S 


CA13 


T 


1/02 


U 


1/03 


V 


S(32 


w 


NMI 


X 


RESET 


Y 


NC 


z 


GND 



241 



This port is used for expansions of the VIC system which require 
access to the address bus or the data bus of the computer. Caution 
is necessary when using the expansion bus, as it is possible to 
damage the VIC by malfunctioning user equipment. 

The signals available on the connector are as follows: 



NAME 


PIN # 


DESCRIPTION 




GND 


1 


System ground 


CDO 


2 


Data bus bit 




GDI 


3 


Data bus bit 1 


CD2 


4 


Data bus bit 2 ' 


CD3 


5 


Data bus bit 3 




CD4 


6 


Data bus bit 4 




CDS 


7 


Data bus bit 5 




CD6 


8 


Data bus bit 6 




CD7 


9 


Data bus bit 7 




BLK1 


10 


8K decoded RAM/ROW! block 1 , (?i $2000 

(active low) 




BLK2 


11 


8K decoded RAM/ROM block 2, (S S4000 


[ 






(active low) 


BLK3 


12 


8K decoded RAM/ROM block 3, fa S6000 




13 


(active low) 

8K decoded ROM block 5. (a SAOOO 




BLK5 






14 


(active low) 

1 K decoded RAM block, @ S0400 




RAMI 






15 


(active low) 

1 K decoded RAM block @ SOBOO 




RAM2 






16 


(active low) 

1 K decoded RAM block (g SOCOO 




RAM3 








(active low) 




VR/W 


17 


Read Write line from VIC chip 






(high -read, low-write) 




crW 


18 


ReadWrite line from CPU 
(high-read, low-write) 




IRQ 


19 


Interrupt Request line to 6502 {active low) 




(NC) 


20 






+ 5V 


21 






GND 


22 






GND 


A 






CAO 


B 


Address bus bit 




CA1 


C 


Address bus bit 1 




CA2 


D 


Address bus bit 2 




CA3 


E 


Address bus bit 3 




CA4 


F 


Address bus bit 4 





242 



Address bus bit 5 

Address bus bit 6 

Address bus bit 7 

Address bus bit 8 

Address bus bit 9 

Address bus bit 10 

Address bus bit 11 

Address bus bit 12 

Address bus bit 13 

I/O block 2 (located at $9600) 

I/O block 3 (located at S9C00) 

Phase 2 system clock 

6502 Non Maskable Interrupt (active low) 

6502 RESET pin (active low) 



RAM Signals— RAM 1 , 2, and 3 are active low signais which are 
used to decode memory placed in the 3K block from $0400 to 
S1000. Each of the RAIVl signais controls a IK block of memory. 
When a RAM signal goes low, it indicates that the block of memory 
it controls is being addressed. 



CA5 


H 


CA6 


J 


CA7 


K 


CAS 


L 


CA9 


M 


CA10 


N 


CA11 


P 


CA12 


R 


CA13 
1/02 


S 

T 


1/03 


U 


S/02 
NM1 


V 

w 


RESET 


X 


(NC) 
GND 


Y 

2 



BLK Signals — The four block signals are also for memory 
expansion of the system. In this case, how ever, each decodes a 
different 8K block of memory. As with the RAM signals, each is 
active low. Blocks 1 , 2, and 3 can be used for either RAM or ROM, 
Memory in those locations can (and will) be used by BASIC. 
Memory in Block 5, however, should be ROM, as this area is not 
accessible to BASIC. If RAM is placed here it can only be utilized by 
machine language programs. 

IMPOR TANT N OTE: If the additional memory is added to the V/C 
using the BLOCK decoding signals, memory added by using the 
RAM signals can not be used by BASIC for storage of BASIC text. 
This is because BASIC deman ds a co ntinuous area for programs. 
With additional memory in the BLOCK decoded areas, the screen 
move s to SI 000, breaking up the area. Memory placed in those 
RAM areas can still be used by machine language programs, 

IRQ — This is the interrupt request line. The VIC uses this internally 
for keyboard scan and the system clock. 

RESET— When this line is grounded, it causes a COLD START of 
the VIC. Everything is RESET, including memory, so any program 
in the VIC at that time is destroyed. 



243 



NWll— When this line is grounded, it causes a VIC WARM START 
(just like RUN STOP-RESTORE). 

Address Bus — The address bus controls what memory ioation or 
I/O device the VIC will read tram or write to. Only 1 4 bits appear on 
the connector, even though the address bus size is 16 bits, 
because two bits are decoded into the BLOCK and I/O signals. 

Data Bus — The data bus is used by the VIC to move data to or from 
memory or I/O devices. 

I/O Signals^These two signals can be used to add additional I/O 
devices to the VIC. The IEEE adapter from Commodore uses these 
signals. 

Read/Write Signals — These signals inform the memory or the 
device being addressed whether the VIC wants to write data or read 
data. If the signal is high, a read is expected. If the signal is Eow, a 
write is desired. 

There are two R/W signals available on the expansion port. One 
(CR/W) is connected to the 6502. The other (VR,W) is connected to 
the VIC chip. Memory expansion will normally use the VR/W signal. 
Other devices may need the CR W signal. 



WHAT HAPPENS WHEN MEMORY IS 
EXPANDED 

The VIC comes with 5K of random access memory (RAM) 
located from to 1 023 (SOOOO to S03FF) for operating system use, 
4096-8192 (S1000 to S1FFF) which is BASIC program area, and 
from 38400 to 3891 1 (S9600 to S97FF) which Is COLOR memory 
area. 

When additional memory is added the VIC screen location, color 
memory location, or the start of BASIC might change. 

Start of Start of Start of Start of 

Added MemoryScreen Color Memory BASIC 

1024-4095 7680(S1E00) 38400(59600) 1024(30400) 

8192 on up 4096 (81 000) 37888 (S9400) 4608 ($1200) 

($2000-3FFF) 

($4000-5FFF) 

(S6000-7FFF) 

The VIC has 2 areas to add additional memory— a 3K space from 
1024 to 4095 (S0400 to $OFFF) and a 24K section from 8192 to 

244 



f 



32767 (S2000 to S7FFF), When the large expansion area is used, 
BASIC cannot use the 3K area as program area. 

When memory is added in the3K area, the BASIC program area 
will start at the beginning of the new RAM area. The screen will still 
begin at 7680 and color memory will still begin at 38400. However, 
the start of BASIC will be at 1024. 

The VIC chip cannot access any of this new memory, so screen 
memory and programmable character memory must be in VIC 
internal memory (4096 to 8191). 

Memory is added to the larger expansion area in 8K blocks, 
beginning at 8192 ($2000). BASIC demands a continuous area for 
programs. This is why the screen is moved — otherwise, the video 
screen will be in the middle of your program. The same reason 
prevents the 3K RAM area from being used by BASIC when 
additional memory is added to the large expansion area. However, 
machine language programs can still use this area though. 

The start of BASIC will begin at 4608 ($1200), the video screen 
will start a 4096 (SI 000) and color memory will start at 37888 
{S9400). 

See Section 3 for the formulas to use to calculate the screen start 
address. If you want your programs to work on any VIC memory 
configuration, your program must use these formulas In POKEs 
and PEEKS to the screen. The best way to use this is at the 
beginning, set a variable to the start of screen memory and one to 
the start of color memory. Then, do any POKEs or PEEKs to the 
screen relative to those variables. (Example: if C is the start of the 
screen, to put an 'A^ on the first line on the 10th column of the 
screen, type: POKEC-h10,1.) 



245 



GAME CONTROLLERS 



L 



USING A JOYSTICK ON THE VIC 

Like all other input and output, the joysticks are controlled using 
the VIC'S 6522 Versatile Interface Adapters (VI As). The 6522 is a 
very versatile and complex device. Fortunately, it isn't necessary to 
delve deeply into the mysteries of the 6522 VIA to read the 
joysticks. 

Each 6522 has two Input/Output ports, called port A and port B. 
Each of the ports has a control register attached, called the DATA 
DIRECTION REGISTER (DDR). This highly important register 
controls the direction of the port. By using this register you can use 
the port for input, output, or both at the same time. To set one bit of 
the port to output, set the corresponding bit of the Data Direction 
Register to 1 . To set a bit ot the port for input, set the corresponding 
bit of the DDR to 0. For example, to set bit 7 of port A to input, and 
the rest of the bits to output, poke 127 in the DDR for port A. 

To read the joystick, one port (and one DDR) of each of the 6522 
VIAs on the VIC must be used. 

The joystick switches are arranged as follows: 



TOP 



FIRE BUT- 
TON 
Switch 4 
(FR) 



Switch 
(SO) 



r 



Switch 2 
(S2) 



! 

Switch 3 

I [S3) 

! 

I 



(SI) 

Switch 1 

Switch 0, Switch 1 , Switch 2, and the Fire button can be read from 
VIA #1, which is located beginning at location S9110. Switch 3 
must be read from the other 6522 (VIA #2) which is located 
beginning at location S9120. 



246 



L 



L 



Now, the key locations for the joystick are as follows; 



HEX 


DECIMAL 


PURPOSE 


9113 


37139 


Data ciirection register for VO port A on 
VIA#1 


9111 


37137 


Output register A 
Bit 2 Joy switch 
Bit 3 Joy switch 1 
Bit 4 Joy switch 2 
Bit 5 Fire button 


9122 


37154 


Data direction register tor I/O port B on 
VIA #2 


9120 

f 


37152 


Output register B 
Bit 7 Joy switch 3 



To read the joystick inputs, you first set the ports to input mode by 
setting the DDR to 0. This can be done by a POKE. Then the value 
of the switches can be read by two peeks. Sounds easy, right? 
There is only one problem . . . VIA#2 is also used for reading the 
keyboard. Setting the DDR can mess up the keyscan rather badly, 
So you have to make sure you restore the DDR to the original 
condition if you want to use the keyboard aftenA/ards. 

To make things really easy, you can use the following program. 
Lines 10 to 25 are initialization. The rest of the program, beginning 
at line 9000, can be called as a subroutine whenever you want to 
read the joystick. 

10 DIM JS(2,2):POKE37139,0:DD = 37r54:PA-37137: 
PB = 37152 

20 FORI-0TO2:FORJ = 0TO2:READJS (JJ):NEXTJJ 
25 DATA-23, -22, -21,-1,0,1,21.22,23 

30 GOSUB9000:PRINT JS(X-r t^Y-f 1):GOTO30 

9000 POKEDD,127:S3= -((PEEK(PB)AND128) = 0): 
POKEDD,255 

9010P = PEEK{PA):S1--{(PAND8)-0):S2 = ((PAND16) = 0} 
:S0 = ((PAND4) = 0) 

9020FR=-({PAND32) = 0):X = S2-rS3:Y = S0-rS1:RETURN 

The variables SO, S1 , 82, and S3 will be normaliy, and will be 
set to 1 {or - 1 ) when the joystick is pointed in that direction. Two of 

247 



the variables will be set to 1 on diagonal moves. FR will be 1 when 
the firing button is pressed, othenA/ise, 

The AND function Is used to pick out one bit of the joystick port. 
The bits are numbered from 7 (most significant bit) to (least 
significant bit). By ANDing the 6522 port with a number whose 
value is a power of two, a single bit is selected. (For example, to pick 
bit 3, AND using 2,3 or 8)- 

The JS array in the program is set up to make moving around the 
screen using the joystick easy. The numbers in the DATA 
statement of line 25 can easily be changed for other purposes. For 
example . . . 

To "decode" the joystick in this pattern: 



TOP 



FIRE 





7 I 1 

6—8—2 

5 ! 3 

4 

The data statement should be changed to: 
25 DATA 7.0,1.6.8.2,5,4,3 

USING PADDLES ON THE VIC 

The paddles are read using both the VIC chip and the VIC's 6522 
Versatile Interface Adapters (VlAs). 

The values of the paddles are read through the VIC chip. There 
are two registers, one for each paddle, which will contain the 
current value of the paddle. This data will be in digitized form, as a 
value from to 255. 

The switches on each paddle are read from the VIA chips. Each 
VIA has two INPUT OUTPUT ports, called PORT A and PORT B. 
Each of the ports has a control register attached, called the DATA 
DIRECTION REGISTER (DDR). This register controls the direction 
of the port. By using this register you can use the port for input, 
output, or both at the same time. To set one bit of the port to output, 
set the corresponding bit of the Data Direction Register to 1 . To set 
a bit of the port for input, set the corresponding bit of the DDR to 0. 
For example, to set bit 7 of pon A to input, and the rest of the bits to 
output, poke 127 in the DDR for port A, 

To read the paddle switches, one port (and one DDR) of each of 
the 6522 VIAs on the VIC must be used. 

248 



The joystick switches are arranged as follows: 

Paddle X Paddle Y 

(S2) {S3) 

Switch 2 can be read from VIA #1 , which is located beginning at 
location S91 1 0. Switch 3 must be read from the other 6522 (VIA ^2) 
which is located beginning at location S9120. 

Now, the key locations tor the paddle are as follows; 



HEX 


DECIMAL 


PURPOSE 


9008 


36872 


Digitized value of PADDLE X 


9009 


36873 


Digitized value of PADDLE Y 


9113 


37139 


Data direction register for I/O port A on 
VIA#1 



9111 37137 Output register A 

Bit 4 PADDLE SWITCH X 

91 22 37154 Data direction register for I/O port B on 

VIA #2 

9120 37152 Output register B 

Bit 7 PADDLE SWITCH Y 

To read the paddle inputs, you first set the ports to input mode by 
setting the DDR to 0. This can be done by a POKE. Then the value 
of the switches can be read by two peeks. Sounds easy, right? 
There is only one problem . . . \/IA#2 is also used for reading the 
keyboard. Setting the DDR can mess up the keyscan rather badly. 
So you have to make sure you restore the DDR to the original 
condition if you want to use the keyboard afterwards. 

To make things really easy, you can use the following program. 
Lines 10 to 25 are Initialization. The rest of the program, beginning 
at line 9000. can be called as a subroutine whenever you want to 
read the paddle. 

10 POKE37139,0:DD = 37154;PA = 37137;PB = 37152 

20 PX = 36S72:PY = 36873 

30 GOSUB9000:PRINT PEEK {PX);PEEK (PY);X:Y:GOTO30 

9000POKEDD,127:Y = -((PEEK{PB)AND12a) = 0):PO 
KEDD,255 

9010 X = -{{PEEK(PA)AND16) = 0):RETURN 

The variables X and Y wiil be normafly, and will be set to 1 when 
that paddle button is pressed. 

249 



The AND function is used to picl< out one bit of the paddle port. 
The bits are numbered from 7 (most significant bit) to (least 
significant bit). By AN Ding the 6522 port with a number winose 
value is a power of two, a single bit is selected. (For example, to pick 
bit 3, AND using 6.) 

USING A LIGHT PEN ON THE VIC 

One of the benefits of using the ViC chip as the controller for the 
VIC 20 is that it is easy to add certain input^output devices for 
games and educational software. It is as easy to add a light pen as it 
is to add game paddies and joysticks. I 

The principle behind the light pen Is simple. Basically, the pen is a [ 

light detector, set to detect either the presence or absence of light. 
The television picture is not put on the screen all at once — rather, it p 

is put on the screen one row at a time, scanning from left to right 
very quickly, ^ 

When the scan passes the area where the pen is. a signal is sent 
to the VIC chip. When this signal is received, the VIC chip, which I 

keeps track of where the scan line is at any particular moment, will 1 

record the exact location of the scan in two registers, 36870 {9006 
HEX) for the X direction and 36871 (9007) in the Y direction. You 
can read and use this information in your programs. 

The light pen trigger is connected to pin 6 of the game port. The 
light pen trigger input can also be reached from pin 7 of the user 
port. Note that you can't use a joystick and a light pen at the same 
time, because the same line that is used as the light pen trigger 
input is used as the joystick fire button input (you would get false 
readings). 

The VIC chip constantly keeps track of the scan position on the 
television in two registers. When the light pen trigger input is 
brought low, the VIC freezes the two registers. You can then read 
and use this information. After reading the two registers, the trigger 
line will be cleared, so that scan information can be placed again in 
the two registers. 



[ 



250 



RS-232 INTERFACE 
DESCRIPTION — BUILT-IN software 

GENERAL OUTLINE 

The VIC has a built-in RS-232 interface for connection to any 
RS-232 modem, printer, or other device. To connect the device to 
the VIC a cable is required, as well as some programming. 

RS-232 on the VIC is standard RS-232 format, but the voltages 
are TTL levels (0 to 5V) rather than the normal RS-232 ^1 2 to 1 2 volt 
range. The cable between the VIC and the RS-232 device should 
take care of the voltage conversions needed. The Commodore VIC 
RS-232 interface cartridge handles this properly. 

The RS-232 interface software can be accessed from BASIC or 
from the KERNAL for machine language programming. RS-232 on 
the BASIC level uses the normal BASIC commands: OPEN. 
CLOSE, CMD, INPLIT#, GET#. PRINT#, and the reserved 
variable ST. INPUT#( CHRIN for the machine language program* 
mers in the audience) and GET#( GETiN) fetch data from the 
receiving buffer, while PRINT# (chrout) and CMD place data into 
the transmitting buffer. The use of these commands (and 
examples) will be described more fully later. 

The RS-232 KERNAL byte bit level handlers run under the 
control of the 6522 device timers and interrupts. The 6522 
generates NMI requests for RS'232 processing. This allows 
background RS-232 processing to take place during BASIC and 
machine language programs. There are built-in hold-offs in the 
KERNAL cassette and serial bus routines to prevent disruption of 
data storage transmission by the NMI's generated by the RS-232 
routines. During cassette or serial bus activities data cannot be 
received from RS-232 devices. Because these hold-offs are only 
local (assuming care is taken in programming) no interference 
should result. 

There are two buffers in the VIC RS-232 interface to help prevent 
loss of data when transmitting or receiving RS-232. The VIC 20 
RS-232 KERNAL buffers consist of two firslHn.tirst-Dut (FIFO) 
buffers, each 255 bytes long , at the top of memory. The OPENing of 
an RS-232 channel automatically allocates 512 bytes of memory 
for these buffers. If there is not enough free space beyond the end 
of your BASIC program no error message will be printed, and the 
end of your program will be destroyed. SO BE CAREFUL! 

These buffers are automatically removed by the CLOSE 
command. 

251 



OPENING AN RS-232 CHANNEL 

Only one RS-232 channel should be open at any time; a second 
OPEN statement will cause the buffer pointers to be reset. Any 
characters in either the transmit buffer or the received buffer will be 
lost. 

Up to 4 characters may be sent in the filename field. The first two 
are the control and command register characters: the other two are 
reserved for future system options. Baud rate, parity, and other 
options can be selected through this feature. 

No error-checking is done on the control word to detect a 
nonimplemented baud rate, so that any illegal control word wiil 
cause the system output to operate at a very slow rate (below 50 
baud), 

BASIC SYNTAX 

OPENlf,2,0V'<control registerxcommand regtster>'* 

If— Norma! logical file id (1-255). If [f>127 then linefeed follows 
carriage return. 

<control regtster> — Single byte character {see Figure 1) 
(required to specify baud rate) 

<command register> — Single byte character (see Figure 2) 
(this character is NOT required) 
KERNAL ENTRY 

OPEN (SFFGO)— See KERNAL spec, for more information 
on entry conditions and instructions. 



NOTE 
IMPORTANT: In a BASIC program, the RS-232 OPEN com- 
mand should be performed before using any variable or DIM 
statement, since an automatic CLR is performed when an 
RS-232 channel is OPENed {because of the allocation of 512 
bytes at the top of memory.) Also remember that your program 
will be destroyed if 51 2 bytes of space are not available at the 
time of the OPEN statement. 



L 



L 



GETTING DATA FROM RS-232 CHANNEL 

When getting data, the VIC receiver buffer will hold 255 
characters before a buffer overflow. This is indicated in the RS-232 
status word {ST from BASIC, rsstatfrom machine language). If this 
occurs, all characters received during a full buffer condition are lost. 
Obviously it pays to keep the buffer as clear as possible- 



252 



If you wish to receive RS'232 data at high speeds (BASIC can 
only go so fast, especially considering garbage collects. This can 
cause the receiver buffer to overflow), you will have to use machine 
language routines to handle the data bursts. 



m\s SiHS 



STOP BITS - 

0-1 STOP BIT 
1-2 STOP BITS 



WORD LENGTH- 



BIT 


DATA 
WORD LENGTH 


6 


5 








8 BITS 





1 


7 BITS 


1 





6 BITS 


1 


1 


5 BITS 



UNUSED 







1— 1 








1-^ 


p* 


BAUD RATE 














USER RATE [Nil 













50 BAUD 













75 












110 





1 








134.5 





1 







150 





1 







300 





1 






600 













1200 












(1800) 2400 












2400 











3500 [Nl] 




1 1 








4800 [Nl] 




1 







7200 [Nl] 




1 







9600 [Nl] 




1 






19200 [Nl] 



Figure 4-1. Control register. 



BASIC SYNTAX 

Recommended: GET#lf,<string variable> 
NOT Recommended: INPUT#lf,<varfable list> 



253 



PARITY OPTIONS 



BIT 

7 


BIT 
6 


BIT 
5 


OPERATIONS 


- 


- 





PARITY DISABLED, NONE 
GENERATED/RECEIVED 








1 


ODD PARITY 
RECEIVER/TRANSMITTER 





1 


1 


EVEN PARITY 
RECEIVER/TRANSMITTER 


1 ' 





1 


MARK TRANSMITTED 
PARITY CHECK DISABLED 


1 


1 


1 


SPACE TRANSMITTED 
PARITY CHECK DISABLED 



DUPLEX 

0-FULL DUPLEX 
I^HALF DUPLEX 

UNUSED 
UNUSED 
UNUSED 



L 



"-HANDSHAKE 

0-3 LINE 
1-X LINE 



L 
[ 
L 



Figure 4*2. Command regfster 



KERNAL ENTRIES 

CHKIN (SFFC6)— See Section 3 for more information on entry 
and exit conditions, 

GETIN (SFFE4) — See Section 3 for more information on entry 
and exit conditions. 

CHRIN (SFFCF)— See Section 3 for more information on entry 
and exit conditions. 



254 



L 
L 



NOTES 

If the word length is less than 8 bits, all unused bit(s) will be 
assigned a value of zero. 

If a GET# does not find any data in the buffer, the character *"' (a 
null) is returned. 

If INPUT# is used, then the system will hang until a non-null 
character and a following carriage return is received. Thus, if the 
CTS or DSR !ine{s) disappear during character INPUT#, the 
system will hang in a RESTORE-only state. This is why the 
INPUT# and CHRIN routines are NOT recommended. 

The routine CHKIN handles the x-line handshake which follows 
the EIA standard (August 1979) for RS-232-C interfaces, (The 
RTS> and DCD lines are implemented with the VIC computer 
defined as the Data Terminal device,} 



r 



255 



SENDING DATA TO AN RS-232 CHANNEL 

When sending data, the output buffer can hold 256 characters 
before a full buffer hold-off occurs. The system will wait in the 
CHROUT routine until transmission is allowed or the RUN STOP- 
RESTORE keys are used to recover the system through a WARM 
START. 

BASIC SYNTAX 

CMD If — acts same as in BASIC spec. 
PRINT#lf,< variable list> 



KERNAL ENTRIES 

CHKOUT (SFFC9)— See Section 3 for more information on entry 
and exit conditions. 

CHROUT (SFFD2) — See Section 3 for more information on entry 
conditions. 



L 



NOTES 

IMPORTANT: There is no carriage-return delay built into the output 
channel so a normal RS-232 printer cannot correctly print, unless 
some form of hold-off (asking the VIC to wait) or internal buffering is 
implemented by the printer. The hold-off can easily be implemented 
in your program. If a CTS (x-fine) handshake is implemented, the 
VIC buffer will fill and hold off more output until transmission is 
allowed by the RS-232 device. 

The routine CHKOUT handles the x-line handshake, which 
follows the EIA standard (August 1979) for RS-232-C interfaces. 
The RTS, and DCD lines are implemented with the VIC defined as 
the Data Terminal Device. 



CLOSING AN RS-232 DATA CHANNEL 

Closing an RS-232 file discards all data in the buffers at the time 
of execution (whether or not it had been transmitted or printed out), 
slops all RS-232 transmitting and receiving, sets the RTS and Sout 
lines high, and removes both RS-232 buffers. 



r 



BASIC SYNTAX 

CLOSE If 



256 



KERNAL ENTRY 

CLOSE (SFFC3) — See Section 3 for mom information on entry 

and exit conditions. 



NOTE 

Care should be taken to ensure all data is transmitted before 
closing the channel. A way to check this from BASIC is: 

100 IF ST=0 AND (PEEK(37151} AND 64)= 1 GOTO 100 
110 CLOSE if 



Table 4-1, USER-PORT LINES 



(6522 DEVICE #1 loc S911 0-911 F) 
PIN 6522 IN/ 

ID ID DESCRIPTION EIA ABV OUT 

MODES 



c 


PBO 


D 


PB1 


E 


PB2 


F 


PB3 


H 


PB4 


J 


PBS 


K 


PB6 


L 


PB7 


B 


CB1 


M 


CB2 


A 


GND 


N 


GND 



RECEIVED DATA 
REQUEST TO SEND 
DATA TERMINAL READY 
RING INDICATOR 
RECEIVED LINE SIGNAL 
UNASSIGNED 
CLEAR TO SEND 
DATA SET READY 
RECEIVED DATA 
TRANSMITTED DATA 
PROTECTIVE GROUND 
SIGNAL GROUND 



(BB) Sin IN 1 2 
(CA) RTS OUT 1*2 
(CD}DTR0UT1*2 



(CE) Rl IN 


3 


(CF) DCD IN 


2 


( } XXX IN 


3 


(CB) CTS IN 


3 


(CC) DSR IN 


2 


(BB) Sin IN 


1 2 


(BA) Sout OUT 1 2 


(AA) GND 


1 2 


(AB) GND 


1 23 



MODES 



1)— 3-LINE INTERFACE (Sin,Sout,GND) 

2)— X-LINE INTERFACE (Full handshaking) 

3)— USER AVAILABLE ONLY (Unused/unimplemented in code.) 

* — These lines are held high during 3-LINE mode. 



*Note; PB6 CLEAR TO SEND is not implemented and must 
be read with a short machine language routine. 



257 



i7][6][51[4][31[2][1 ][0] (Machine lang.— rsstat) 

: — -PARITY ERROR BIT 
FRAMING ERROR BIT 



-RECEIVER BUFFER 
OVERRUN BIT 



-UNUSED BIT 

-CTS SIGNAL MISSING 
BIT 

UNUSED BIT 

DSR SIGNAL MISSING BIT 

BREAK DETECTED BIT 



Figure 4-3. RS-232 Status Register 



NOTES 

If the BIT = 0, then no error has been detected. 

The RS-232 status register can be read from BASIC using the 
variable ST, 

If ST is read by BASIC or by using the KERNAL READST routine 
the RS-232 status word is cleared upon exit. !f multiple uses of the 
STATUS word are necessary the ST should be assigned to another 
variable, i.e. 

SR = ST: REM ASSIGNS ST TO SR 

The RS-232 status is read (and cleared) only when the RS-232 
channel was the last external I/O used. 



SAMPLE BASIC PROGRAM 

10 REM THIS PROGRAM SENDS AND RECEIVES DATA 
TO/FROM A SILENT 700 TERMINAL MODIFIED FOR PET 
ASCII 

20 REM Tl SILENT 700 SET-UP: 300 BAUD. 7-BIT ASCII. 
MARK PARITY, FULL DUPLEX 



258 



30 REM SAME SET-UP AT COMPUTER USING 3-LINE 
INTERFACE 

100OPEN2.2,3,CHRS(6 + 32)-i-CHRS(32+128): REMOPEN 
THE CHANNEL 

110 GET#2.AS: REM TURN ON THE RECEIVER CHANNEL 
(TOSS A NULL) 
200 REM MAIN LOOP 

210 GET BS: REM GET FROM COMPUTER KEYBOARD 
220 IF BS<>""' THEN PRINT#2,B$;: REM IF A KEY 
PRESSED, SEND TO TERMINAL 

230 GET#2.CS: REM GET A KEY FROM THE TERMINAL 
240 PRINT B$;C$;: REM PRINT ALL INPUTS TO THE 
COMPUTER SCREEN 

250 SR = ST: IF SR-0 THEN 200: REM CHECK STATUS, IF 
GOOD THEN CONTINUE 
300 REM ERROR REPORTING 
310 PRINT -ERROR: "; 
320 IF SR AND 1 THEN PRINT "PARITY" 
330 IF SR AND 2 THEN PRINT "FRAME" 
340 IF SR AND 4 THEN PRINT "RECEIVER BUFFER FULL" 
350 IF SR AND 128 THEN PRINT "BREAK" 
360 IF (PEEK(37151) AND 64) = 1 THEN 360: REM WAIT 
UNTIL ALL CHARS TRANSMITTED 
370 CLOSE 2: END 



RECEIVER/TRANSMITTER BUFFER BASE 
LOCATION POINTERS 

S00F7-RIBUF A two byte pointer to the Receiver Buffer base 
location. 

S00F9-ROBUF A two byte pointer to the Transmitter Buffer base 
location. 

The two locations above are set up by the KERNAL OPEN 
routine, each pointing to a different 256 byte buffer. They are 
de-allocated by writing a zero into the high order bytes, (S00F8 and 
S00F9), which is done by the KERNAL CLOSE entry. They may 
also be allocated de-allocated by the machine language program- 
mer for his/her own purposes, removing 'creating only the buf[er(s) 
required. Both the OPEN and CLOSE routines will not notice that 
their jobs might have been done already. When using a machine 
language program that allocates these buffers, care must be taken 
to make sure that the top of memory pointers stay correct, 
especially if BASIC programs are expected to run at the same time. 

259 



r 

L 



ZERO-PAGE MEMORY LOCATIONS AND 
USAGE FOR RS-232 SYSTEM INTERFACE 

S00A7-INBIT Receiver input bit temp storage. 

S0OA8-BITC1 Receiver bit count in. 

S00A9-RINONE Receiver flag Start bit check. 

$OOAA-RIDATA Receiver byte buffer/assembly location. 

SOOAB-RIPRTY Receiver parity bit storage. 

$0084-81175 Transmitter bit count out. j 

S0085 — NXTBIT Transmitter next bit to be sent | 

S0086- RODATA Transmitter byte buffer/disassembly location. 

Alt the above zero page locations are used locally and are only . 

given as a guide to understand the associated routines These 
cannot be used directly by the BASIC or KERNAL level ' 

programmer to do RS-232 type things. The system RS-232 
routines must be used. | 

NONZERO-PAGE MEMORY LOCATIONS 

AND USAGE FOR RS-232 SYSTEM INTER- f 

FACE ' 

General RS-232 storage: I 

S0293-M51CTR Pseudo 6551 control register (see Figure 4-1). *^ 

$0294-M51CDR Pseudo 6551 command register {see Figure 
4-2). I 

S0295-M51 AJ8 Two bytes following the control and command I . 

registers in the file name field. (For future use.) 

$0297"RSSTAT The RS-232 status register (see Figure 4-3). 

S0298-BITNUIVI The number of bits to be sent received, 

$0299-BAUDOF Two bytes that are equal to the time of one bit 
cell. (Based on system clock baud rate.) 

$029B-RID8E The byte index to the end of the receiver FIFO 
buffer. 

S029C'RID8S The byte Index to the start of the receiver FIFO 
buffer. 

S029D-ROD8S The byte index to the start of the transmitter 
FIFO buffer. 

S029E-RODBE The byte index to the end of the transmitter Fl FO 
buffer. 



L 
[ 



260 



] 
] 



1 
] 
] 



] 
J 



1 
1 



APPENDIX A 



ABBREVIATIONS FOR BASIC KEYWORDS 

As a time saver when typing in programs and commands, VIC BASIC 
allows the user to abbreviate most keywords. The abbreviation for the word 
PRINT is a question mark. The abbreviations for the other words are made 
by typing the first one or two letters of the keyword, foifowed by the 
SHIFTed next letter of the word, if the abbreviations are used in a program 
iine, the keyword wiJi LIST in the ionger form. Note that some of the 
keywords when abbreviated incfude the first parenthesis, and others do 
not. 



Dommand 


Abhreviatifln 


Looks like 

\hh on screerv 


Command 


Ahbre^fation 


LOQkS IJks 
this m sere BO 


Aes 

AND 
ASC 


A ^^Q 

A ^^Q S 


* 1 

A V 


INPUT* 
liT 

LEFTS 


> |^^3 N 

I ^^Q E 

LE i^^Q F 


L 1 
LE 


CLOSE 


A ^^Q - 




LIST 
LOAD 


^ ^^Q I 

'- ^^^1 

M ^^Q 1 


L 


CLf^ 


C^^Q L 


c 


NEXT 


N ^^Q^ 


N 


CMD 


C^^Q V 


^N 


NOT 


N ^^Q 


N 


CONT 


C^^Q Q 


^ ! 


OPEN 


0^^3 ^ 





DATA 


D^^Q A 


0'^ 


PEEK 


p ^^Q ^ 


^r 


DEF 

DIM 


O^^Q E 





POKE 
PPiNT 


p ^^Qo 

? 


p 

7 


EMD 


E ^^Q N 


^R 


PRINT pr 


p ^^Q "^ 


P 


EXP 


E Q^Q K 


E + 


mEAO 


R ^^^Q E 


Pi 


FOR 


F |^^9o 


F 


RESTORE 


RE^^Q S 


RE Y 


f^^ 


F ^^Q^ 


^\~ 


RETURN 


RE ^^3 T 


RE 


GET 


G |^^2 E 


G \~^ 


RIGHTS 


R ^^Q > 


R ^^ 


GOSUS 


GO ^^Q S 


GO ¥ 


AND 


n ^^Q N 


^/i 


GOTO 


I^^Q Q 


G 


RUN 


^ ^^3 


^!^ 



263 







Looks like 






Looks like 


Command 


iblsrcviatitin 


this on screen 


Command 


mTsmim 


this OFT screen 


SAVE 


s l^^l A 


s ^ 


SYS 


s ^^Qy 


s M 


SGN 


s lyjmpG 


^E 


TAB 


T ^^Q A 


T ^ 


SIN 


s g^migi 


^ :i_ 


THE^f 


' ^^Q H 


^ 1 


SPC( 


s ^^Qp 


s 


usfl 


u^^^Q ^ 


^!^1 


SQH 


s ^^Qo 


^m 


VAL 


V ^^^S A 


V ^ 


STEP 


ST ^^Qe 


ST ' 


VERIFY 


V ^^Q £ 


^^ 


STOP 


s ^yiigj 


S ' 


WAIT 


wEnm A 


w A 


SIRS 


s- mmn R 


ST 









COLOR CODE TABLE 

Following are the various colors the VIC can display . note 
that colors 8-15 can only be used as a SCREEM COLOR or an 
AUXILIARY COLOR (see pg. 93 for explanation of auxiliary 
colors used in MULTICOLOR MODE). As an example, these 
color numbers are used to POKE a color into "color mem- 
ory" when coloring characters POKEd to the screen. If you 
POKE 7680, 81 this places a ' ball'^ on the screen but it will be 
"invisible ■ until you add the color by typing POKE 38400, 
(BLACK). Try POKE38400,2 for RED, etc. (Numbers 8-15 can- 
not be used as character colors) 



BLACK 





WHITE 


1 


RED 


2 


CYAN 
PURPLE 


3 
4 


GREEN 


5 


BLUE 


6 


YELLOW 


7 


ORANGE 


8 


LT. ORANGE 
PINK 


9 
10 


LT, CYAN 


11 


LT. PURPLE 


12 


LT. GREEN 
LT, BLUE ~ 


13 
14 


LT. YELLOW 


15 



264 



APPENDIX B 



SCREEN & BORDER COLOR 
COMBINATIONS 

You can change the screen and border colors of the VIC anytime, in or 
out of a program, by typing 

POKE 36879. X 

where X is one of the numbers shown in the chart below. POKE 36879, 27 
returns the screen to the normal cofor combination, which is a CYAN 
border and white screen. 



Try typing POKE 36879,8. Then type CTRL ^^^ and you have 
white letters on a totally black screen! Try some other combinations. This 
POKE command is a quick and easy way to change screen colors in a 
program. 



BORDER 


SCREEN 


BLK 


WHT 


RED 


CYAN 


PUR 


CRN 


BLU 


YEL 


BLACK 


8 


9 


10 


11 


12 


13 


14 


15 


WHITE 


24 


25 


26 


27 


28 


29 


30 


31 


RED 


40 


41 


42 


43 


44 


45 


46 


47 


CYAN 


56 


57 


58 


59 


60 


61 


62 


63 


PURPLE 


72 


73 


74 


75 


76 


77 


78 


79 


GREEN 


ee 


89 


90 


91 


92 


93 


94 


95 


BLUE 


104 


105 


106 


107 


108 


109 


110 


m 


YELLOW 


120 


121 


122 


123 


124 


125 


126 


127 


ORANGE 


136 


137 


135 


139 


140 


141 


142 


143 


LT. ORANGE 


152 


153 


154 


155 


156 


157 


158 


159 


PINK 


168 


169 


170 


171 


172 


173 


174 


175 


LT. CYAN 


184 


185 


186 


187 


188 


189 


190 


191 


LT PURPLE 


200 


201 


202 


203 


204 


205 


206 


207 


LT. GREEN 


216 


217 


218 


219 


220 


221 


222 


223 


LT BLUE 


232 


233 


234 


235 


236 


^7 


233 


239 


LT. YELLOW 


240 


249 


250 


251 


252 


253 


254 


255 



265 



APPENDIX C 



L 

L 



TABLE OF MUSICAL NOTES 



APPROX. 



APPRDX, 



»iOTE 


VALUE 


NOTE 


VALUE 


C 


135 


G 


215 


C# 


143 


G^ 


217 





m 


A 


219 


D# 


151 


A# 


221 


E 


159 


a 


223 


F 


163 


c 


225 


m 


167 


c/^ 


227 


G 


175 


D 


228 


G# 


179 


D* 


229 


A 


183 


E 


231 


A# 


187 


F 


232 


B- 


1S1 


f^ 


233 


e 


135 


G 


235 


c# 


199 


G# 


236 


D 


201 


A 


237 


D# 


203 


A^ 


238 


E 


m 


B 


239 


F 


209 


C 


240 


F# 


212 


Cff 


241 



L 



SPEAKER COMMANDS; 


WHERE X CAN BE: 


FUNCTJON; 


POKE 36878. X 


Olo 15 


sets volume 


POKE 36374. X 


128 to 255 


plays tone 


POKE 36875. X 


128 to 255 


plays tone 


POKE 35876. X 


128 10 255 


plays tone 


POKE 36877, X 


128 lo 255 


piays "noise" 



[ 



266 



L 



APPENDIX D 



SCREEN DISPLAY CODES 



The following chart lists all of the characters built-in to the VfC 20 
character sets. It shows which numbers should be POKEd into screen 
memory (locations 7630 to SI 85) to get a desired character. Also, it shows 
what character corresponds to a number PEEKed from the screen. 

The two character sets are available, but only one set at a time. This 
means that you cannot have characters from one set on the screen at the 
same time you have characters from the other set displayed. The sets are 
switched by holding down the SHIFT and COMMODORE keys 
simultaneously. This actually changes the 2 bit in memory location 36869, 
which means that the statement POKE 36869, 240 will set the character 
set to upper case, and POKE 36869, 242 switches to lower case. 

If you want to do some serious animation, you will find that it is easier to 
control objects on the screen by POKEIng them into screen memory (and 
erasing them by POKEing a 32, which is the code for a blank space, Into the 
same memory location), than by PRINTing to the screen by using cursor 
control characters. 

Any number shown on the chart may also be displayed in REVERSE. 
Reverse characters are not shown, but the reverse of any character may 
be obtained by adding 128 to the numbers shown. 

NOTE; SEE SCREEN MEMORY MAP APPENDIX E 

If you want to display a heart at screen location 7800, find the number of 
the characteryou want to display there (in this case a heart) in this chart . . . 
the number for the heart is 83 . . . then type in a POKE statement with the 
number of the screen location (7800) and the number of the symbol (83) 
like this: 

POKE 7800.83 

A white heart should appear In the middle area of the screen. Note that ft 
will be invisible if the screen is white. Try changing the position by chang- 
ing the larger number, or type in different symbols using the numbers from 
the chart. 

If you want to change the COLOR of the symbol being displayed, consult 
the Color Codes Memory Map in Appendix E which lists the COLOR 
NUMBERS for EACH MEMORY LOCATION. In other words, to get a 
different colored symbol at a particular location, this requires another 
POKE command. 



267 



For example, to get a red heart, type the foHowing: 
POKE 38520. 2 




This changes the color of the symbol at location 7800 to red. If you had a 
different symbol there, that symbol would now be red. You can display any 
character in any of the available colors by combining these two charts. 
These POKE commands can be added in your programs and are very 
effective especially in animation^and also provide a means to PEEK at 
certain locations If you are doing sophisticated programming such as 
bouncing a ball, which requires this information. 



SCREEN CODES 



SET 1 


SET 2 


POKE 


SET 1 SET 2 


POKE 


SET 1 SET 2 POKE 


@ 







R 


r 


18 


S 


36 


A 


a 


1 


s 


s 


19 


% 


37 


B 


b 


2 


T 


t 


20 


& 


38 


C 


c 


3 


U 


u 


21 


r 


39 


D 


d 


4 


V 


v 


22 


( 


40 


E 


e 


5 


w 


w 


23 


) 


41 


F 


f 


6 


X 


X 


24 


* 


42 


G 


g 


7 


Y 


y 


25 


+ 


43 


H 


h 


8 


z 


2 


26 


, 


44 


1 


i 


9 


[ 




27 


- 


45 


J 


i 


10 


£ 




28 


. 


46 


K 


k 


11 


] 




29 


/ 


47 


L 


1 


12 


T 




30 





48 


M 


m 
n 




13 
14 
15 


*- 




31 
32 
33 


1 

2 
3 


49 


N 


SPACE 


50 


O 


I 


51 


P 


P 


16 






34 


4 


52 





q 


17 


# 




35 


5 


53 



L 



268 



SET1 


SET 2 


POKE 


SET 1 SET 2 


POKE 


SET1 


SET 2 


POKE 


6 




54 


n 





79 


S«l 




104 


7 




55 


□ 


p 


80 


r. 


3^ 


105 


8 




56 


m 


Q 


81 


J 




106 


9 




57 




R 


82 


ih 




107 


:: 




58 


V 


S 


83 


j 




108 


i 




59 


II 


T 


84 


H 




109 


< 




60 


\-C 


U 


85 


1 

n 




110 


= 




61 


^ 


V 


86 






111 


> 




62 


o 


w 


87 


\ r 




112 


7 




63 


1*1 


X 


88 


■J2 




113 


H 




64 


m 


Y 


89 


: 1 ■ 




114 


W 


A 


65 


♦ 


z 


90 






115 


m 


B 


66 


H- 




91 


1 




116 


H 


c 


67 


SJ 




92 


r 




117 




D 


68 , 


m 




93 


[] 




118 




E 


69 


TT ;i;i 


94 


^ 




119 


u 


F 
G 
H 


70 
71 
72 


ni ! 


SS 


95 
96 
97 


n 


^ 


120 


n 


SPACE 


121 


n 


[] 


122 


K 


1 


73 


^ 




96 


kj 




123 


^ 


J 


74 


1 1 




99 


"■ 




124 


K 


K 


75 


u 




100 


J 




125 


U 


L 


76 


D 




101 


F1 




126 


N 


M 


77 


M 




102 


"t 




127 


\A 


N 


78 


\ 




103 









Codes from 1 28-255 are reversed Images of codes 0-1 27. 



269 



APPENDIX E 



SCREEN MEMORY MAPS 



Use this appendix to find the menrtory location of any position on the 
screen. Just find the position in the grid and add the numbers on the row 
and column together. For example, if you want to poke the graphic "ball" 
character onto the center of the screen, add the numbers at the edge of row 
11 and column 11 (7900+ 10) for a total of 7910. If you poke the code for a 
ball (81 , see Appendix D) into location 7910 by typing POKE 7910,81. a 
white ball appears on the screen. To change the color of the ball (or other 
character), find the corresponding position on the color codes memory 
map, add the row and column numbers together {38620 - 1 0, or 38630) for 
the cofor code and type a second poke statement. For example, if you poke 
a color code into this location, POKE 38630,3 the bali will change color to 
cyan. Note that when POKEing, the character color numbers are one less 
than the numbers on the color keys — as shown below. 



L 



Abbreviated List of Color Codes: 




Code 


Color 





Black 


1 


White 


2 


Red 


3 


Cyan 


4 


Purple 


5 


Green 


e 


Blue 


7 


Yellow 



[ 

I 



270 



L 



1 2 3 4 5 6 7 8 9 10 1 1 12 13 14 15 16 17 18 19 20 21 



7680 
7702 
7724 
7746 
7768 
7790 
7812 
7834 
7856 
7878 
7900 
7922 
7944 
7966 
7988 
8010 
8032 
8054 
8076 
8098 
8120 
8142 
8164 






Screen Character Codes 

I 2 3 A S 6 7 8 9 10 11 12 13 14 15 16 17 18 19 aO 21 



38400 

38422 
38444 
38466 
36488 
38510 
38532 
38554 
38576 
38598 
38620 
38642 
38664 
38666 
38708 
38730 
38752 
36774 
38796 
38818 
38840 
38862 
38884 







































,^— 


^^" 


~ — 
















































































































































































1 












































n 














































































































































































































































































































~^ 


1 


























































































































































































































— 






































~ 


' " 


"^~" 






































































































































(J 







1 




u 


_ 


_ 


_ 





























Color Codes Memory Map 



271 



APPENDIX F 



ASCII AND CHR$ CODES 



L 



This appendix shows you what characters will appear If you PRINT 
CHRS {X), for all possible values of X. It will also show the values obtained 
by typing PRINT ASC ("x") where x is any characlsr you can type- This Is 
useful In evaluating the character received in a GET statement, converting 
upper/lower case, and printing character-based commands (like switch to 
upper/lower case) that could not be enclosed in quotes. 



PRINTS CHRS 


PRINTS 


CHRS 


PRINTS 


CHRS 


PRINTS 


CHRS 







22 


1 


44 


B 


66 


1 




23 


- 


45 


C 


67 


2 




24 


. 


46 


D 


68 


3 




25 


/ 


47 ' 


E 


69 


4 




26 





48 


F 


70 


^^^K 5 




27 


1 


49 


G 


71 


6 


^^m 


28 


2 


50 


H 


72 


7 


B 


29 


3 


51 


1 


73 


DtStBLEsQIQB 


^^ 


30 


4 


52 


J 


74 


ENABLES QQQg 


m 


31 


5 


53 


K 


75 


10 


^^Q 


32 


6 


54 


L 


76 


11 


1 


33 


7 


55 


M 


77 


12 


" 


34 


8 


56 


N 


78 


^^^Q 13 


# 
$ 
% 


35 
36 
37 


9 


57 
58 
59 



P 
Q 


79 

80 i 




nKframi I't 


15 


16 


& 


38 


<Z 


60 


R 


62 : 


B^ 17 


• 


39 


= 


61 


S 


83 


£^ 18 


{ 


40 


I> 


62 


T 


84 ; 


ffmuH T" 


) 


41 


? 


63 


U 


85 


@ 20 


* 


42 


@ 


64 


V 


86 


21 


+ 


43 


A 


65 


w 


87 



272 



PRINTS 


CHRS 


PRIMTS 


CKRS 


PRIKTS 


CHRS 


1 PRINTS 


CHRS 


X 


88 


U 


114 


f8 


140 


m 


166 


Y 

Z 
( 


89 
90 
91 


n 


115 
116 
117 




ni4i 

1 142 
143 


^ 

c 


167 
168 
169 


I^^MUUbl 


mnmSfft- 




£ 


92 


^ 


118 


m 


144 


J 


170 


1 


93 


o 


119 


ffi^ 


145 


ih 


171 


T 


94 


t* 


120 




146 


m 


172 


♦— 


95 


m 


121 




147 


L 


173 


— 


96 


♦ 


122 


H 


148 


1 
n 


174 


> 


97 


FR 


123 




149 


^ 


175 


m 


98 


£] 


124 




150 


n 


176 


H 


99 


m 


125 




151 


a 


177 


— 


100 


■JT 


126 




152 


T 


178 


"— 


101 


H 


127 




153 


H 


179 


— 


102 




128 




154 


1 


180 


u 


103 




129 




155 


r 


181 




104 




130 


^^ 


156 


n 


182 


hj 


105 




131 


^^ 


157 


n 


183 


V 


106 




132 


m 


158 


p 


184 


y 


107 


fi 


133 




159 


■ : 

U 


185 


u 


108 


f3 


134 


^^Q 


160 


U 


186 


N 


109 


f5 


135 


1 


161 


■ 


187 


•\/\ 


110 


f7 


136 




162 


■ 


188 


n 


111 


f2 


137 


~ 


163 


M 


189 


n 


112 


f4 


138 




164 


■ 

i 


190 


m 


113 


f6 


139 


1 


165 


s 


191 



CODES 


192-223 


SAME AS 


96-127 


CODES 


224-254 


SAME AS 


160-190 


CODE 


255 


SAME AS 


126 



27^ 



ASCII Character Codes (decimal)' 



L 



ASCII 




ASCII 




ASCJI 




CJQdt 


Chz racier 


Code 


Character 


Code 


Characler 


000 


NULL 


043 


^ 


066 


V 


m 


SON 


044 


, 


087 


w 


002 


STX 


045 


- 


066 


X 


003 


ETX 


046 




069 


Y 


004 


EOT 


047 




090 


2 


d05 


ENQ 


048 





091 


[ 


006 


ACK 


049 


1 


092 


bkslash 


007 


BEL 


050 


2 


093 


] 


OOB 


es 


051 


3 


094 


up arrow 


009 


HT 


0S2 


4 


095 


back arr 


010 


LF 


053 


5 


096 


Space 


011 


VT 


054 


5 


097 


a 


012 


FF 


055 


7 


098 


b 


ots 


CR 


056 


e 


099 


c 


014 


SO 


057 


9 


100 


d 


015 


SI 


05S 




101 


© 


01$ 


DLE 


059 


; 


102 


f 


6l7 


DC1 


060 


< 


103 


g 


oia 


DC2 


061 


= 


104 


h 


019 


DC3 


062 


> 


105 


i 


m 


DC4 


063 


? 


106 


J 


021 


NAK 


064 


(w 


107 


k 


022 


SYN 


065 


A 


106 


1 


023 


ETB 


066 


B 


109 


m 


024 


CAN 


067 


C 


110 


n 


02S 


EM 


05S 


D 


in 





026 


SUB 


069 


B 


112 


P 


027 


ESCAPE 


070 


F 


113 


q 


920 


FS 


071 


G 


lU 


r 


m 


GS 


072 


H 


115 


s 


tm 


RS 


073 


1 


1lD 


1 


031 


US 


074 


J 


117 


u 


0^2 


SPACE 


075 


K 


118 


V 


033 


1 


076 


L 


119 


w 


034 


'' 


077 


M 


120 


X 


035 


# 


078 


N 


121 


y 


036 , 


S 


079 





122 


z 


037 


% 


oeo 


P 


123 


1 


030 


& 


061 


Q 


124 


< 


039 


■ 


082 


R 


125 


ff 


w 


( 


063 


S 


126 


> 


041 


) 


064 


T 


127 


DEL 


042 


' 


0B5 


U 







L 
[ 
L 
[ 



'VIC character cocJes differ from ASCII codes. This table Is provided as a reference for 
ASC 1 1 / V I C conve rsions. 



r 



274 



APPENDIX G 



DERIVING MATHEMATICAL FUNCTIONS 

Functions that are not intrinsic to VIC BASIC may be calculatecJ as 
follows: 



FUNCTION 


VIC BASIC EQUIVALENT 


SECANT 


SEC(X) = 1/COS(X) 


COSECANT 


GSC(X) = 1/SIN(X) 


COTANGENT 


C0T(X) = 1/TAN(X) 


INVERSE SINE 


ARCS fN(X) = ATN(X SQR{ - XrX + 1 }) 


INVERSE COSINE 


ARCCOS(X)- -ATN(XSQR 




(-X''X + 1))+ Tr/2 


INVERSE SECANT 


ARCSEC(X) - ATN{X'SQR{r X - 1 )) 


INVERSE COSECANT 


ARCCSC(X) = ATN (X SQR(X'X - 1 )) 




- (SGN(X) - r tr/2 


INVERSE COTANGENT 


ARCOT{X) = ATN(X)-r77/2 


HYPERBOLIC SINE 


SINH(X) = (EXP(X) - EXP{ - X))/2 


HYPERBOLIC COSINE 


COSH(X) - {EXP(X) - EXP{ - X))/2 


HYPERBOLIC TANGENT 


TANH{X) - EXP( - X)/(EXP(x) + EXP 




(-X)r2-1 


HYPERBOLIC SECANT 


SECH(X) = 2/(EXP(X) - EXP( - X)) 


HYPERBOLIC COSECANT 


CSCH(X) = 2/(EXP(X) - EXP( - X)) 


HYPERBOUC COTANGENT 


COTH(X) = EXP{-X).(EXP(X) 




-EXP(-X)r2 + 1 


INVERSE HYPERBOLIC SINE 


ARCSINH(X)-L0G(X*SQR(X*X-^1)) 


INVERSE HYPERBOLIC COSINE 


ARCCOSH(X)- LOG(X-SQR(X^X- 1)) 


INVERSE HYPERBOLIC TANGENT 


ARCTANH(X) = L0G((1 ^X) (1 - X)) 2 


INVERSE HYPERBOLfC SECANT 


ARCSECH(X) = LOG({SQR 




(-X*X+1)^1/X) 


INVERSE HYPERBOLIC COSECANT 


ARCCSCH(X) = LOG({SGN(X)^SQR 




(X*X-1/x) 


INVERSE HYPERBOLIC COTAN- 


ARCC0TH(X) = L0G{(X*1).(X-tJ)/2 


GENT 





275 



APPENDIX H 



ERROR MESSAGES 



This appendix contains a complete list of the error messages generated 
by the VIC. with a description of the causes. 

BAD DATA String data was received from an open file, hut the program 

was expecting numeric data. 

BAD SUBSCRIPT The program was trying to reference an element of an 

array whose number is outside of the range spectfied in the DIM statement, 

CAN'T CONTINUE The CONT command will not work, either because 

the program was never RUN, there has been an error, or a line has been 

edited. 

DEVICE NOT PRESENT The required I/O device was not available for 

an OPEN, CLOSE. CMD, PRINT#. INPUT#. or GET#. 

DIVISION BY ZERO Division by zero is a mathematical oddity and not 

ailowed. 

EXTRA IGNORED Too many items ot data were typed in response to an 

INPUT statement. Only the first few items were accepted. 

FILE NOT FOUND If you were looking for a file on tape, and 

END-OF-TAPE marker was found. If you were looking on disk, no fiie with 

that name exists, 

FILE NOT OPEN The file specified in a CLOSE, CMD, PRINT#. 

1NPUT#. or GET#, must first be OPENed. 

FILE OPEN An attempt was made to open a file using the number of an 

already open fiie. 

FORMULA TOO COMPLEX The string expression being evaluated 

should be split into at least two parts for the system to work with. 

ILLEGAL DIRECT The INPUT statement can only be used within a 

program, and not in direct mode. 

ILLEGAL QUANTITY A number used as the argument of a function or 

statement is out of the allowable range. 

LOAD There is a problem with the program on tape. 

NEXT WITHOUT FOR This is caused by either incorrectly nesting loops 

or having a variable name in a NEXT statement that doesn't correspond 

with one in a FOR statement, 

NOT INPUT FILE An attempt was made to INPUT or GET data from a file 

which was specified to be for output only. 

NOT OUTPUT FILE An attempt was made to PRINT data to a file which 

was specified as input only. 

OUT OF DATA A READ statement was executed but there is no data left 

unREAD in a DATA statement. 



276 



L 
L 



L 



r 



L 



OUT OF MEMORY There Is no more RAM available for program or 

variables. This may also occur when too many FOR loops have been 

nested, or when there are too many QOSUBs in effect. 

OVERFLOW The result of a computation is larger than the largest 

number allowed, which is 1.70141884E-H38. 

REDIM'D ARRAY An array may only be DIMensioned once. If an array 

variable fs used before that array is DIM^d, an automatic DIM operation is 

performed on that array setting the number of elements to ten, and any 

subsequent DIMs will cause this error. 

REDO FROM START Character data was typed in during an INPUT 

statement when numenc data was expected. Just re-type the entry so that 

it is correct, and the program will continue by itself. 

RETURN WITHOUT GOSUB A RETURN statement was encountered, 

and no GOSUB command has been issued. 

STRING TOO LONG A string can contain up to 255 characters. 

SYNTAX A statement is unrecognizable by the VfC. A missing or extra 

parenthesis, misspelled keywords, etc. 

TYPE MISMATCH This error occurs when a number is used in place of a 

string, or vEce-versa. 

UNDEF'D FUNCTION A user defined function was referenced, but it has 

never been defined using the DEF FN statement. 

UNDEF'D STATEMENT An attempt was made to GOTO or G0SU8 or 

RUN a line number that doesn't exist. 

VERIFY The program on tape or disk does not match the program 

currently in memory. 



277 



APPENDIX I 



CONVERTING PROGRAMS TO VIC 20 
BASIC 

If you have programs written in a B AS IC other than VIC 20 BASIC, some 
minor adjustments may be necessary before running them with VIC 20 
BASIC. The following paragraphs specify things to look for when 
converting BASIC programs. 

String Dimensions 

Delete all statements that are used to declare the length of strings. A 
statement such as DIM AS (\,J), which dimensions a string array for J 
elements of length I, should be converted to the VIC 20 BASIC statement 
DIM AS (J). 

Some BASICS use a comma or ampersand for string concatenation. 
Each of these must be changed to a plus sign, which is the operator for VIC 
20 BASIC string concatenation. 

In VIC 20 BASIC, the MID$, RIGHTS, and LEFTS functions are used to 
take substrings of strings. Forms such as AS(I) to access the Ith character 
in A$, or A$(I,J) to take a substring of AS from position I to position J, must 
be changed as follows: 

Other BASIC VIC 20 BASIC 

AS(1) = X$ AS = LEFTS (AS.I-1) - XS + MIDS (AS.I-r1) 

A$(I.J)-XS AS = LEFTS (ASJ-1)*X$^ MIDS (AS,J^1) 

Multiple Assignments 
To set B and C equal to zero, some B ASICs allow statements of the form: 

10 LET B-C = 

VtC 20 BASIC would interpret the second equal sign as a logical 
operator and set B equal to - 1 if C equaled 0. Instead, convert this 
statement to two assignment statements: 

10 C = 0:B = 

Multiple Statements 

Some BASICS use a backslash (\) to separate multiple statements on a 
line. With V;C*20 BASIC, be sure all statements on a line are separated by 
a colon (:), 

Mat Functions 

Programs using the MAT functions available in some BASICs must be 
rewritten using FOR . . . NEXT loops to execute properly. 

Character Tokens 

To conserve user space, BASIC keywords are translated to 1 -character 
tokens. The token values are shown in the following table. 

278 



L 



L 



r 



TOKENS FOR VIC 20 BASfC 





BASIC 




BASIC 


Token 


keyword 


Token 


keyword 


128 


END 


167 


THEN 1 


129 


FOR 


168 


NOT 


130 


NEXT 


169 


STEP 


131 


DATA 


170 


_ 


132 


INPUT# 


171 


_ 


133 


INPUT 


172 


* 


134 


DIM 


173 


/ 


135 


READ 


174 




136 


LET 


175 


AND 


137 


GOTO 


176 


OR 


138 


RUN 


177 


> 


139 


IF 


178 


s 


140 


RESTORE 


179 


< 


141 


GOSUB 


180 


SGN 


142 


RETURN 


181 


INT 


143 


REM 


182 


ABS 


144 


STOP 


183 


USR 


145 


ON 


184 


FRE 


146 


WAIT 


185 


POS 


147 


LOAD 


186 


SQR 


148 


SAVE 


187 


RND 


149 


VERIFY 


188 


LOG 


150 


DEF 


189 


EXP 


151 


POKE 


190 


COS 


152 


PRINT* 


191 


SIN I 


153 


PRINT 


192 


TAN 


154 


CONT 


193 


ATN 


155 


LIST 


194 


PEEK 


156 


CLR 


195 


LEN 


157 


CMD 


196 


STRS 


158 


SYS 


197 


VAL 


159 


OPEN 


196 


ASC 


160 


CLOSE 


199 


CHRS 


161 


GET 


200 


LEFTS 


162 


NEW 


201 


RIGHTS 


163 


TAB( 


202 


MIDS 


164 


TO 


203 


GO 


165 


FN 


204 




166 


SPC( 




(1)?SYNTAX ERROR 



Note: (1 ) The token after used token produces this error when listed. 



tm 



APPENDIX J 

PINOUTS FOR INPUT/OUTPUT DEVICES 

Here is a picture of the I/O ports on the VIC: 




L 

r 
r 



1) Game I/O 4) Serial I/O (disk) 

2) Memory Expansion 5) Cassette 

3) Audio and Video 6) User Port (modem) 



Game I/O 




PIN# 


TYPE 


NOTE 


1 


JOY0 




2 


J0Y1 




3 


J0Y2 




4 


J0Y3 




5 


POTY 




6 


LIGHT PEN 




7 


+ 5V 


MAX.lOOmA 


8 


GND 




9 


POTX 





[ 



r 



280 



Memory Expansion 



12 3 4 5 6 7 8 9 10 111213 14 1516 17 1819 202122 




ABCDEFHJ KLMNPRSTUVWXYZ 



PIN# 


TYPE 


1 


GND 


2 


CDO 


3 


GDI 


4 


CD2 


5 


CD3 


6 


CD4 


7 


CDS 


8 


CD6 


9 
10 
11 


CD7 
BLK1 


BLK2 


PIN# 


TYPE 


A 


GND 


B 


CAO 


C 


CA1 


D 


CA2 


E 


CA3 


F 


CA4 


H 


CAS 


J 


CA6 


K 


CA7 , 


L 


CA8 


M 


CAS 



PIN# 


TYPE 


12 




BLK3 


13 

14 
15 


BLK5 


RAMI 


RAM2 


16 


RAMS 


17 


VR/W 


18 
19 


CR/W 
IRQ 


20 


NC 


2t 


-5V 


22 


GND 



PfN # 



N 
P 
B 
S 
T 
U 
V 

w 

X 
Y 

2 



TYPE 



CA10 

CA11 

CA12 

CA13 

1/02 

1/03 

S02 



NMI 



RESET 

NC 

GND 



281 



Audb^^ldeo 




PIN# 


TYPE 


NOTE 


1 
2 
3 
4 
5 


-5V REG 
GND 
AUDIO 
VIDEO LOW 
VIDEO HIGH 


10mA MAX 



L 



Serial I/O 



/^ 




^ 




\^3^ 



PIN # ' 


TYPE 


1 
2 
3 
4 
5 
6 


SERIAL SRQ IN 

GND 

SERIAL ATN IN/OUT 

SERIAL CLK IN/OUT 

SERIAL DATA IN/OUT 

RESET 



[ 



Cassette 



12 3 4 5 6 



A B C D E F 



PIN# 


TYPE 


A-1 


GND 


B-2 


t5V 


C-3 


CASSETTE MOTOR 


D-4 


CASSETTE READ 


E-5 


CASSETTE WRITE 


F-6 


CASSETTE SWITCH 



r 



^2 



User 10 



1 2 3 4 5 6 7 a 9 10 11 12 



ABCDEFHJKLMN 



PIN # 


TYPE 


NOTE 


PIN # 


TYPE 


NOTE 


1 


GND 




A 


GND 




2 


*5V 


100mA MAX, 


B 


CB1 




3 


RESET 




C 


POO 




4 


JOYO 




D 


PB1 




5 


JOYl 




E 


PB2 




6 


JOY2 ' 




F 


PB3 




7 


LIGHT PEN 




H 


PB4 




8 


CASSETTE SWITCH 




J 


PBS 




9 


SERIAL ATN IN 




K 


PB6 




10 


*9V 


100mA WAX. 


L 


PB7 




11 


t9V 




M 


CB2 




12 


GND 




N 


GND 





283 



\ 



APPENDIX K 



VIC PERIPHERALS & ACCESSORIES 

Here is a list including just a few of the growing number of 
peripfierals, accessories and progrannmlng tools wtiich are 
available from Cornmodore for your VIC 20: 

COMMODORE DATASSETTE ... for loading your own 
programs and replaying inexpensive pre-recorded tape progranns. 

VIC 1540 SINGLE DISK DRIVE . . . stores up to 170K oi data on 
a floppy diskette, for fast, tiigh-capacity data storage and 
retrieval 

VIC GRAPHIC PRINTER , , , 80 column dot matrix printer for 
making paper printouts; prints VIC graphics, [etters, numbers and 
programmable characters, 

VICMODEM . , . Commodore's exclusive "affordable" modem 
on cartridge turns tfie VIC into a telecommunications terminal. 
Originate/answer, direct connect, 300 baud. VICTERM I tape 
included. 

VIC 3K MEMORY EXPANDER . . . 3K memory expansion on 
cartridge. 

VIC 8K MEMORY EXPANDER . , , 8K RAM memory expansion 
cartridge, 

VIC 16K MEMORY EXPANDER . , . 16K RAM memory expansion 
cartridge. 

RS232 TERMINAL INTERFACE . , , adapter cartridge for RS232 
applications (connects to user port), 

IEEE-48a INTERFACE CARTRIDGE . . . for IEEE applications a 
PET/CBM accessor res. 

GAME JOYSTICK , . . for playing Commodore games on 
cartridge or tape. 

TWO PLAYER PADDLES ... for games and other programs. 

LIGHTPEN ... for screen-touch programs such as 
computerized drawing. 

PROGRAMMING AIDS 

PROGRAMMER'S AID CARTRIDGE . . . more than 20 BASIC 
program editing commands. 

VIC SUPEREXPANDER CARTRIDGE . . . graphics plotting, 
music, 3K expansion all on one cartridge. 

VICMON , . . Machine Language Monitor for writing/editing 
machine code programs. 

PROGRAMMABLE CHARACTER/GAMEGRAPHICS EDITOR , . . 
tape program lets you create your own VIC characters, symbols, 
alphabets. 

TEACH YOURSELF PROGRAMMING SERIES , . . self-teaching 
books and tapes for VIC owners who want to learn more about 
programming. Begins with INTRODUCTION TO PROGRAMMING, 
Part I. 



284 



INDEX 



Abbreviating sound comnnands, 96 
Abbreviations. BASIC commands, 

79. 263 
ABS function, 41 
ASC function, 41 
Accumulator. 126, 140 
Addition, 62 
Addressing, VIC, 113 
Adventure games, ix 
AND operator. 65, 66 
Applications, ix 
Arithmelic formulas, 62, 275 
Arithmetic operators. 62 
Arrays, 60, 81 
ASC function, 41 
ASCII & CHR$ Codes, 272 
Asterisk (multiplication), 64 
ATN function, 42 
Auxiliary coior, 93, 217 

B 

BASIC, 1 

abbreviations, 79, 263 

commands, 5 

keyword codes, 121 

locations, 116-120, 118-119 

operators, 62, 68 

statements, 14 

variables, 58, 80 
Beginning machine code, 132, 168 
Bit mapping, 88 
Bit patterns, 93 
Boolean operators, 62 
Boolean truth table, 66 
Border color. 93 
Buffer, 77 
Bus 

address, 109 

control. 111 

data, 111 
Byte, 131 

C 

Calculator mode, 75 
Ctiaracter generator ROM, 82 
Ctiaracter memory, 82 
Ctiaracter size, 215 
Chess (Sargon It), x 
CHRS function, 40, 42 
CHR$ codes, 272 
Chips 

6502 chip, 113 

VIC chip, 113 
CLR statement, 14 
CLR/HOMEkey, 73 
Clock, 179. 184 5,204 
CLOSE statement, 35 



CMD statement, 35 
Color 

auxiliary^ 217 

border, 217 

keys, 29 

memory map, 

register, 93 

screen and border. 
Columns, video. 214 
Commands. BASIC, 5 
Commodore Key. 73 
Communication, x 
Concatenation, 58, 62, 69 
Connecting tfie VIC (see owners 

guide) 
CONT command, 5 
Control bus, 111 
Converting PET to VIC, 278 
cosine function, 43 
CRSR keys, 28, 73. 74 
Crunching BASIC programs, 79 
CTRL key, 29 

D 

DATA statement. 15,81,86 

DELete key, 73 

Deriving math functions, 275 

Device addressing, 38 

Device number addressing, 38 

OlMension statement, 17, 61 

Direct mode, 76 

Disk, 8 

E 

Editing programs, 74 
Eliminating spaces, 81 
END statement, IS 
Error messages, 276 
Expansion port, 241, 244 
Expansion RAM/ROM, 118 
Exponent function, 43 
Exponentiation, 63 

F 

Fetch cycle, 109 

Filenames, 70 

Fioating point variabies, 54, 59 

FOR statement, 19 

FOR . .. NEXT loop, 19 

FRE function, 43, 85 

Frequency modulation, 101 

Functional block diagram, 110 

Functions, BASIC, 40 

Function keys. 78 

G 

Game controls. 246 

Game port, 246 

GET statemenL 20, 77 

GET# statement, 36 

GQSUB statement. 20, 81, 133 



L 
[ 
[ 
[ 

[ 
L 

r 

r 



286 



GOTO statement, 22 

Graphics, 82 
character memory, 82 
programmable characters, 82 
high resolution, 87 

Greater than symbol, 64 

H 

Hexadecimal notation, 128, 131, 

170,178 
High resoiulion. 87 
Home position, 73 
Horizontal screen origin, 213 

I 

IEEE'488 Interface, 244 

IF .. . THEN statement. 22 

Immediate mode, 75 

indexed indirect addressing, 135 

Indexing, 34 

Indirect indexed addressing, 134 

Input buffer, 77 

INPUT statement. 24 

iNPUT# statement, 36 

INSert key, 30, 73 

[nslmction set (6502), 140 

fNTeger function, 44 

integer variables, 54, 57 

Interface mode, 213 

Interpreter, BASIC, 119, 125 

I/O guide, 227 

I/O ports, addressing, 113, 184 

1/0 registers, 218 

I/O statements, 35 

I/O status, 49 

IRQ, 243 



Joystick, 246 
Jump table, 138 

K 

Keyboard buffer. 77, 180 

Keywords, 120-121 

KERNAL, 114, 116, 125, 138, 182, 
251,259 

power-up activities, 211 
user callable routines. 184 



LDA (load accumulator), 130 
LEFTS function. 44 
LENgth function, 45 
LET statement, 25 
Light pen, xij, 215, 250 
Line numbers, 79, 120 
LIST command, 6 
LOAD command, 7 
Loan/Mortgage Calculation, xi 
LOGarithm function, 45 
Logical operators, 68 



M 

Machine language programming, 

107, 123 
Machine code 

Memory expansion, 124, 244 
Memory map, 124, 170 
Microprocessor (6502), 109 
MIDS function, 45 
Minus sign, 63 

Mixing sound & graphics, 105 
Multicolor mode, 92 
Multiple speakers, 100 
Multiple statements on a line, 75. 

80 
Music, 95, 96, 232 
Musical note table, 266 
Music, frequency modulation, 101 
Music programming techniques, 98 

H 

NEW command, 9 
NEXT statement, 26 
NOT operator, 68 
Number bases. 123 
Numbers, 54 

O 

Octave comparison chart, 99 

Octaves, 95 

ON statement, 26 

Operators, 62 

OR operator, 68 



Paddles, 216, 246 

Parantheses {in formulas), 64 

PEEK function. 46 

Piano program, 103 

Pin configuration, 213, 241 

Pinouts for I/O devices, 280 

POKE statement, 27 

POS function, 46 

PRINT statement, 28 

PRINTS, 39 

Printer, 236 

Program counter, 126 

Programmable characters, 82, 237 

Programming music. 98 

Programming tips, 71 

Program mode, 75 

Programs, 76 

editing, 73 

line numbering, 78 

Q 

Quote mode, 29 



RAM memory, 85. 109, 111,115 
RAM starting Iccations, 85, 118 
Raster value, 215 



287 



READ statement. 31. 81 

Register, 126 

Relational operators. 62, 64 

REMark statement, 31. 80 

Reserved Words. 60, 120 

Reset, 243, 4 

RESTORE statement. 32 

Return key, 73 

RETURN statement, 32 

Reversed cinaracters. 29, 85. 217, 239 

RIGHTS function. 47 

RND function. 47 

ROM, 109. 114, 115 

Rounding numbers, 54 

Rows, video. 215 

RS-232 interface. 251 

RUN command, 10 

RUN/STOP RESTORE, 4, 25 



SAVE comniand. 10 

Schematic (inside back cover) 

Scientific notation. 56 

Screen & border colors, 265 

Screen display codes, 257 

Screen editing. 73 

Screen formatting, 201 

Screen memory location, 215, 270 

Screen RAM, 115 

Serial bus, 234 

SGN function, 48 

Shift register. 221 

Shortening programs. 79 

SINe function, 48 

Sound commands. 96 

Sound, programming, 95, 216 

Space, 74. 81 

SPC function, 48, 81 

Speakers, 95 

SQR function, 49. 91 

ST numeric value, 50 

Stack, 133. 141 

Stack pointer. 127. 134 

Stan of text. 119: of memory. 124 

Statements, BASIC. 14 

Status function. 49 

Status register. 126 

STOP command. 33. 4 

STR$ function. 50 

String comparisons, 70 

String operations, 57, 70 

String variables. 57 



Subroutines, 138-9 
Subtraction. 62 
Super expander. 94 
SYS statement, 33 
System clock, 204 
System overviev;. 109 

T 

TAB function, 51. 81, 121 

Talking VIC, xiv 

TAM function. 51 

Tl variable, 52 

TIS variable. 52, 77, 179 

Time, setting VIC clock, 52 

Timer, 220 

Top of memory, 119 

True/false testing, 65, 68 

U 

Upper/lower case, 115 

USR function. 52 

User-defined function. 52 

Useful memory locations. 178 

User port, 229 

User program, memory location. 119 

V 

VALue function, 53 

Variables, 58. 80 

Variables, extended names, 60 

VERIFY command, 12 

Versatile interface devices, 109. 111. 

218 
Vertical screen origin, 214 
ViCMON, 127, 135. 137 
VlCTIPs, 85, 96, 103. 105 
Video interface ctiip, 116, 212 
Volume, 95 

W 

WAIT statement, 34 
Warm start. 4 

White noise generator. 104 
Writing machine code, 132 



L 



r 

L 



X Index register, 126 



Y Index register, 126 



Zero page, 133 



288 



o 



ip. THE MICROCOMPUTER 
MAGAZINE 

commodore 

The Commodore Magazine provides a vehicle for sharing the latest product 
information on Commodore systems, programming techniques, hardware 
interfacing, and applications for tlie CBM, PET, SuperPET and VIC 
systems. 

Each issue contains features of interest to anyone that owns, or is thinking 
about, purchasing Commodore equipment: 

• Application anicles examine how users from various backgrounds use 
Commodore computers in business, education, and the home. 

• Columns by leading experts explain ways to get the most out of your 
computer in clear, concise language. 

• The latest news on user clubs, a question/answer hotUne column, and 
reviews of the newest books and software round out the most complete 
magazine devoted exclusively to Commodore computers. 

Readers are also encouraged to submit anicles for publication and share 
their experiences using Commodore computers. 

Commodore Magazine is published 6 times a year by Commodore Business 
Machines, Inc. The subscription fee is S15-(K) for six issues within the 
United Slates and its possessions and S25.00 for Canada and Mexico. 

Commodore Magazine Subscription Form 

Na me : 

Address : 

City: Slate: Zip: 

Renewal Subscription New Subscription, 



Equipment Use: Personal Education Business 

My subscription began with issue number 



Enclosed is a check or money order for $_ 



for issues of Commodore Magazine 

Make checks payable to Commodore Business Machines, Inc, 
681 Moore Road 
King of Prussia, PA 19406 
Attn: Editor, Commodore Magazine 



289 



1 



1 



[ 

L 



/ 



ABOUT THE VIC 20 

PROGRAMMERS REFERENCE GUIDE... 



This easy-to-use manual gives you a ready source of 
information on VIC 20 software and hardware, with 
detailed explanations of each topic and "friendly" 
tips throughout to help you use your VIC 20 to best 
advantage. 



This guide was compiled from information provided 
by Commodore programming staffs working in more 
than half a dozen countries worldwide. 

Whether you're a first-time computerist or an expert 
programmer, you'll find a wide variety of program- 
ming aids available through your Commodore dealer. 
In addition to books and manuals, Commodore 
provides several special programming cartridges and 
the TEACH YOURSELF PROGRAMMING dm) 
instruction series. 



I 
I 

] 

I 
I 
I 



The^VIC PROGRAMMERS REFERENCE GUIDE is 

actually four guides in one. It includes (1) a BASIC 

VOCABULARY GUIDE which explains the complete 

VIC BASIC language instruction set along with (2) a r 

PROGRAMMING TIPS GUIDE with suggestions on I 

how to innprove your programming, (3) a MACHINE 

LANGUAGE PROGRAMMING GUIDE to help you 

talk to the VIC in its '*own** binary/hexadecimal 

language, and (4) a special section on INPUT/ 

OUTPUT OPERATIONS giving you the information 

needed to connect your VIC to special peripherals 

like RS232 devices^ lightpens and others* 



1 
I 
I 
\ 



Cc commodore [ 

^ COMPUTER 

DISTRIBUTED BY 

HoLuord UU, Soms & Co., Inc. 

4300 W, G2nd Street. Indianapolis, Intiiana 4626a USA 

$16.95/21948 ISBN: 0-672-21943-4